获取地图

发送反馈


WMS 接口的 getMap(String version, WMSMapParameter wmsMapParam)方法可通过传入 WMS 版本号和 WMS 地图请求参数获取到地图。SuperMap iServer 定义了 WMSMapParameter 对象类型,用于存储 WMS 地图服务请求参数。

在 WMSClientSample 类中添加 getMap 方法,并在 Main 函数中添加如下代码将图片二进制流输出到一个 png 文件:

    // 获取地图

    public byte[] getMap() {

        byte[] mapBytes = null;

        // 构建 WMS 地图参数对象

        WMSMapParameter wmsMapParameter = new WMSMapParameter();

        // WMS 服务的版本号

        String wmsVersion = "1.1.1";

        // 给 WMS 地图参数对象赋值

        wmsMapParameter.mapName = "China";

        wmsMapParameter.entireBounds = new Rectangle2D(-180, -90, 180, 90);

        wmsMapParameter.bounds = new Rectangle2D(-180, -90, 180, 90);

        wmsMapParameter.layers = new String[1];

        wmsMapParameter.layers[0] = "China_Boundary_ln@China#1";

        wmsMapParameter.srs = "EPSG:4326";

        wmsMapParameter.width = 800;

        wmsMapParameter.height = 400;

        wmsMapParameter.styles = new String[]{""};

        try {

            mapBytes = wmsClient.getMap(wmsVersion, wmsMapParameter);

        } catch (Exception e) {

             e.printStackTrace();

        }

        return mapBytes;

    }

      

    public static void main(String[] args) {

        WMSClientSample wmsClientSample = new WMSClientSample();

        // 输出服务标题和地图标题

        WMSCapabilities capabilities = wmsClientSample.getCapabilities();

        String wmsTitle = capabilities.serviceDescription.title;

        String mapTitle = capabilities.mapTitle;

        System.out.println("WMS Title is " + wmsTitle);

        System.out.println("MapTitle is " + mapTitle);

        

        // 输出地图

        byte[] bytes = wmsClientSample.getMap();

        String fileName = "d:/map.png";

        FileOutputStream outputStream = null;

        try {

            outputStream = new FileOutputStream(fileName);

        } catch (FileNotFoundException e1) {

            e1.printStackTrace();

        }

        try {

            outputStream.write(bytes);

            outputStream.flush();

            outputStream.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

        System.out.println("请在 d:/ map.png 文件中查看地图");

    }

在运行 WMSClientSample 之前,需要保证 http://localhost:8090/iserver/services/map-china400/wms111/China 服务已启动。

编译 WMSClientSample 工程之后,右键 WMSClientSample 工程,选择 Run as > Java Application 运行该 Java 应用程序,控制台会提示查看 d:/map.png。