实现服务组件 |
创建一个 Temperature 服务组件接口及其接口实现类 TemperatureImpl,该服务组件实现获取地图(getMapImage)和获取天气信息(getTemperature)两个方法。
Temperature 服务组件接口定义如下:
package com.supermap.sample.temperature;
public interface Temperature {
String getTemperature();
String getMapImage();
}
TemperatureImpl 接口实现类代码如下:
package com.supermap.sample.temperature;
import java.util.HashMap;
import com.supermap.services.components.commontypes.*;
import com.supermap.services.components.spi.MapProvider;
import com.supermap.services.providers.UGCMapProvider;
import com.supermap.services.providers.UGCMapProviderSetting;
public class TemperatureImpl implements Temperature {
private MapProvider mapProvider = null;
private MapParameter defaultMapParam = null;
private static String outputPath = "../webapps/iserver/output";
private static final String WORKSPACE_CHINA = "../samples/data/China/China100.smwu";
public TemperatureImpl() {
UGCMapProviderSetting mapSetting = new UGCMapProviderSetting();
mapSetting.setWorkspacePath(WORKSPACE_CHINA);
mapSetting.setOutputPath(outputPath);
mapSetting.setOutputSite("http://localhost:8090/iserver/output");
mapSetting.setMaps("China");
mapProvider = new UGCMapProvider(mapSetting);
defaultMapParam = mapProvider.getDefaultMapParameter("China");
this.defaultMapParam.viewer = new Rectangle(new Point(0, 0), new Point(800, 600));
}
@Override
public String getTemperature() {
String temp;
temp = "晴,气温10度";
return temp;
}
@Override
public String getMapImage() {
String imageUrl = null;
if (defaultMapParam != null) {
ImageOutputOption imageOutputOption = new ImageOutputOption();
imageOutputOption.format = OutputFormat.JPG;
imageOutputOption.transparent = false;
ThemeLabel themeLabel = new ThemeLabel();
themeLabel.memoryData = new HashMap<String, String>();
String cityName = "北京";
String temp = cityName + "," + getTemperature();
themeLabel.memoryData.put(cityName, temp);
themeLabel.labelExpression = "NAME";
themeLabel.labelBackShape = LabelBackShape.ROUNDRECT;
Style style = new Style();
style.fillBackColor = new Color(java.awt.Color.MAGENTA.getRed(), java.awt.Color.MAGENTA.getGreen(), java.awt.Color.MAGENTA.getBlue());
style.fillBackOpaque = true;
style.fillForeColor = new Color(java.awt.Color.YELLOW.getRed(), java.awt.Color.YELLOW.getGreen(), java.awt.Color.YELLOW.getBlue());
style.fillGradientMode = FillGradientMode.RADIAL;
themeLabel.backStyle = style;
TextStyle textStyle = new TextStyle();
textStyle.backColor = new Color(java.awt.Color.BLUE.getRed(), java.awt.Color.BLUE.getGreen(), java.awt.Color.BLUE.getBlue());
textStyle.fontWidth = 100000;
textStyle.fontHeight = 100000;
textStyle.align = TextAlignment.MIDDLECENTER;
themeLabel.uniformStyle = textStyle;
DatasetVectorInfo datasetVectorInfo = new DatasetVectorInfo();
datasetVectorInfo.name = "China_Capital_P";
datasetVectorInfo.type = DatasetType.POINT;
datasetVectorInfo.dataSourceName = "China";
UGCThemeLayer themelayer = new UGCThemeLayer();
themelayer.theme = themeLabel;
themelayer.datasetInfo = datasetVectorInfo.copy();
themelayer.visible = true;
themelayer.displayFilter = "NAME = '" + cityName + "'";
this.defaultMapParam.layers.get(0).subLayers.add(true, themelayer);
this.defaultMapParam.center = new Point2D(11000000.0, 3500000.0);
this.defaultMapParam.scale = 0.00000003;
MapImage mapImage = mapProvider.getMapImage(this.defaultMapParam, imageOutputOption);
imageUrl = mapImage.imageUrl;
}
return imageUrl;
}
}
将以上代码编译之后,包含 Temperature.class 文件的 com 目录(%SuperMap iServer_HOME%\samples\code\DSSE\Temperature_SC\bin\com),整个拷贝到 SuperMap iServer Web 应用下,即 %SuperMap iServer_HOME%webapps\iserver\WEB-INF\classes 下(先在该目录下新建 classes 文件夹)。