图例显示
思路:图例是地图展示里常用的辅助展示工具。曾经我采用过将图例截成图片转为2进制保存到数据库里,然后在加载图层时候调取对应图片作为图例展示。但是后来知道geodatabase数据其实就是个空间数据库,如果是通过geodatabase数据加载图层,图斑都是geodatabase数据里的空间数据Geometry根据样式Symbol进行动态渲染,所以这个Symbol如果能提取出来,也就是我们要的图例了。
if (layer instanceof FeatureLayer) {
FeatureLayer featureLayer = (FeatureLayer) layer;
GeodatabaseFeatureTable geoTable = (GeodatabaseFeatureTable) featureLayer.getFeatureTable();
String typeFileid = geoTable.getTypeIdField();
if (typeFileid.equalsIgnoreCase("")) {//TypeIdField没有值
//获取图层中的FeatureTemplate
List templates = geoTable.getFeatureTemplates();
LayerLegendInfo titleLegend = new LayerLegendInfo();
titleLegend.setName(featureLayer.getName());
titleLegend.setType(LayerLegendInfo.Type.TITLE);
legendInfoList.add(titleLegend);
//从templates构造出图例
for (FeatureTemplate template : templates) {
try {
//根据FeatureTemplate生成GdbFeature
//GeodatabaseFeature feature = geoTable.createFeatureWithTemplate(template, null);
//Renderer renderer = featureLayer.getRenderer();
获取符号
//Symbol symbol = renderer.getSymbol(feature);
Symbol symbol = featureLayer.createPopupInfo().createSymbolFromTemplate(template);
//得到代表图例的图片
Bitmap bitmap = createBitmapFromSymbol(symbol, layer);
String name = template.getName();
LayerLegendInfo legend = new LayerLegendInfo();
legend.setName(name);
legend.setType(LayerLegendInfo.Type.ITEM);
legend.setBitmap(bitmap);
legendInfoList.add(legend);
//设置图例的布局和点击事件
// populateTemplateView(layerTemplate,bitmap,template,symbol,(FeatureLayer)layer);
} catch (Exception e) {
e.printStackTrace();
}
}
} else {//TypeIdField有值
List featureTypes = geoTable.getFeatureTypes();
LayerLegendInfo titleLegend = new LayerLegendInfo();
titleLegend.setName(featureLayer.getName());
titleLegend.setType(LayerLegendInfo.Type.TITLE);
legendInfoList.add(titleLegend);
for (FeatureType featureType : featureTypes) {
FeatureTemplate[] templates = featureType.getTemplates();
for (FeatureTemplate template : templates) {
try {
//根据FeatureTemplate生成GeodatabaseFeature
//GeodatabaseFeature feature = geoTable.createFeatureWithTemplate(template, null);
//Renderer renderer = ((FeatureLayer) layer).getRenderer();
获取符号
//Symbol symbol = renderer.getSymbol(feature);
Symbol symbol = featureLayer.createPopupInfo().createSymbolFromTemplate(template);
//得到代表图例的图片
Bitmap bitmap = createBitmapFromSymbol(symbol, layer);
String name = template.getName();
LayerLegendInfo legend = new LayerLegendInfo();
legend.setName(name);
legend.setBitmap(bitmap);
legend.setType(LayerLegendInfo.Type.ITEM);
legendInfoList.add(legend);
//设置图例的布局和点击事件
//populateTemplateView(layerTemplate,bitmap,template,symbol,(FeatureLayer)layer);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
图例显示效果图:
图例显示效果.png