上篇文章讨论了Java调用 SuperMap iServer Java6R的REST API 比较直接,直接自己拼URL,比较费劲,今天发现SuperMap iServer Java6R的SP1版中提供了RestMapProvider 等接口,意味着可以直接调用 iServer-all的 jar包即可。
准备:
1、启动iServer 6R服务。
代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
使用RestMapProviderpackage com.awei20081.restMap;
import com.supermap.services.components.commontypes.*;
import com.supermap.services.providers.*;
public class TestMapProvider {
public final static void main(String[] args) throws Exception {
/***
* 思路如下:
* 1、实例化RestMapProvider对象。
* 2、调用专题图接口,设置专题图。
* 3、调用RestMapProvider对象的getMapImage方法获取图片。
* 4、输出URL,在浏览器中输入此地址,访问。
*/
//实例化一个RestMapProviderSetting
RestMapProviderSetting restMapProviderSetting = new RestMapProviderSetting();
//设置Rest服务的根目录
restMapProviderSetting.restServiceRootURL = "http://localhost:8090/iserver/services/maps/rest/";
//实例化RestMapProvider对象
RestMapProvider restMapProvider = new RestMapProvider(restMapProviderSetting);
//获取默认地图参数
MapParameter defaultMapParam = restMapProvider.getDefaultMapParameter("世界地图");
MapParameter currentMapParam = new MapParameter(defaultMapParam);
// 找到地图对应的图层集合
LayerCollection layerCollection = currentMapParam.layers.get(0).subLayers;
// 清除掉地图中配置的所有图层
layerCollection.clear();
// 新建专题图图层
UGCThemeLayer themeLayer = new UGCThemeLayer();
ThemeGraph themeGraph = new ThemeGraph();
// 使用3维饼图
themeGraph.graphType = ThemeGraphType.PIE3D;
// 使用值作为显示文本
themeGraph.graphTextFormat = GraphTextFormat.VALUE;
// 等级大小使用常量模式
themeGraph.graduatedMode = GraduatedMode.CONSTANT;
// 是否固定大小,true表示不随图缩放
themeGraph.graphSizeFixed = false;
// 显示文本
themeGraph.graphTextDisplayed = true;
// 显示负值
themeGraph.negativeDisplayed = true;
themeGraph.items = new ThemeGraphItem[2];
ThemeGraphItem item0 = new ThemeGraphItem();
// 字段SQKM对应的部分使用红色来表示
item0.graphExpression = "SQKM";
item0.uniformStyle = new Style();
item0.uniformStyle.fillForeColor = new Color(255, 0,0);
item0.caption = "1";
ThemeGraphItem item1 = new ThemeGraphItem();
// 字段SQMI对应的部分使用绿色来表示
item1.graphExpression = "SQMI";
item1.uniformStyle = new Style();
item1.uniformStyle.fillForeColor = new Color(0, 255,0);
item1.caption = "2";
themeGraph.items[0] = item0;
themeGraph.items[1] = item1;
// 设置数据集信息,必须为矢量数据集
DatasetVectorInfo datasetInfo = new DatasetVectorInfo();
datasetInfo.name = "Countries";
datasetInfo.dataSourceName = "World";
datasetInfo.type = DatasetType.REGION;
// 给图层赋值
themeLayer.theme = themeGraph;
themeLayer.datasetInfo = datasetInfo;
themeLayer.opaqueRate = 100;
// 添加到图层集合中,以便显示在地图上
layerCollection.add(themeLayer);
// 不使用图片缓存,每次都会重新出图
currentMapParam.cacheEnabled = false;
// 设置出图大小为800x800
currentMapParam.viewer = new Rectangle(0,0,512,512);
// 设置出图返回格式为文件路径
currentMapParam.returnType = ReturnType.URL;
//实例化图片选项
ImageOutputOption outputOption = new ImageOutputOption();
// 指定出图格式为png
outputOption.format = OutputFormat.PNG;
//调用getMapImage接口,获取图片对象
MapImage mapImage = restMapProvider.getMapImage(currentMapParam,outputOption);
//输出图片地址
System.out.println(mapImage.imageUrl.toString());
}
}
得到输出结果:
http://localhost:8090/iserver/services/maps/rest/maps/%E4%B8%96%E7%95%8C%E5%9C%B0%E5%9B%BE/image.png?viewer=%7B%22leftTop%22%3A%7B%22y%22%3A0%2C%22x%22%3A0%7D%2C%22rightBottom%22%3A%7B%22y%22%3A512%2C%22x%22%3A512%7D%7D&cacheEnabled=false&rectifyType=BYCENTERANDMAPSCALE&clipRegion=%7B%22center%22%3Anull%2C%22id%22%3A0%2C%22style%22%3Anull%2C%22parts%22%3Anull%2C%22points%22%3Anull%2C%22type%22%3A%22REGION%22%7D&clipRegionEnabled=false&customEntireBoundsEnabled=false&angle=0.0&antialias=true&colorMode=DEFAULT&dynamicProjection=false&markerAngleFixed=false&maxVisibleTextSize=1000.0&maxVisibleVertex=3600000&minVisibleTextSize=0.3&overlapDisplayed=true&paintBackground=true&textAngleFixed=false&textOrientationFixed=false¢er=%7B%22y%22%3A27.5734580668%2C%22x%22%3A96.1736997712%7D&scale=2.563206512274041E-8&layersID=tu45quuz_fc28ec230a514c45a2217b8db661b191&imageFormat=PNG&noLayers=true&transparent=false
输入浏览测试,得到效果图如下
![image image](https://images.cnblogs.com/cnblogs_com/awei20081/201107/201107142233137978.png)
此处简单使用了RestMapProvider接口,后续再对程序进行升级扩展,做出一个C/S小系统。