图例是地图展示里常用的辅助展示工具。曾经我采用过将图例截成图片转为2进制保存到数据库里,然后在加载图层时候调取对应图片作为图例展示。但是后来知道geodatabase数据其实就是个空间数据库,如果是通过geodatabase数据加载图层,图斑都是geodatabase数据里的空间数据Geometry根据样式Symbol进行动态渲染,所以这个Symbol如果能提取出来,也就是我们要的图例了。根据这个思路,我找了找,果然找到了对应的方法,于是记录下来。
首先我们还是先初始化地图,并且加载图层:
mMapView=(MapView)findViewById(R.id.mapview); rvDownload=(RecyclerView)findViewById(R.id.rv_tuli); String path=StorageUtil.getSDCardRootPath(getApplicationContext())+"/ydyzt/data/GD_TDYT.geodatabase"; Geodatabase localGdb=null; try { localGdb = new Geodatabase(path); }catch (Exception e){ e.printStackTrace(); } if (localGdb != null) { for (GeodatabaseFeatureTable gdbFeatureTable : localGdb.getGeodatabaseTables()) { if (gdbFeatureTable.hasGeometry()){ FeatureLayer layer = new FeatureLayer(gdbFeatureTable); mMapView.addLayer(layer); } } } mMapView.setMapBackground(0xFAFAFA, 0xffffff, 0.0f, 0.0f);//地图背景
然后根据我们加载的图层获取对应的符号,然后将符号按照点,线,面类型转换成对应的Bitmap,而图例名称则取GeodatabaseFeatureTable里对应FeatureTemplate的名称,就构成了我们的图例:
Layer layer=mMapView.getLayer(0); tuliBeanList=new ArrayList<>(); if(layer instanceof FeatureLayer){ String typeFileid= ((GeodatabaseFeatureTable)(((FeatureLayer)layer).getFeatureTable())).getTypeIdField(); if(typeFileid.equalsIgnoreCase("")){//TypeIdField没有值 //获取图层中的FeatureTemplate Listtemplates= ((GeodatabaseFeatureTable)(((FeatureLayer)layer).getFeatureTable())).getFeatureTemplates(); //从templates构造出图例 for(FeatureTemplate template :templates){ try { //根据FeatureTemplate生成GdbFeature GeodatabaseFeature feature= ((GeodatabaseFeatureTable)(((FeatureLayer)layer).getFeatureTable())).createFeatureWithTemplate(template, null); Renderer renderer = ((FeatureLayer) layer).getRenderer(); //获取符号 Symbol symbol = renderer.getSymbol(feature); //得到代表图例的图片 Bitmap bitmap=createBitmapFromSymbol(symbol,layer); String name=template.getName(); TuliBean bean=new TuliBean(name,bitmap); tuliBeanList.add(bean); //设置图例的布局和点击事件 // populateTemplateView(layerTemplate,bitmap,template,symbol,(FeatureLayer)layer); } catch (Exception e) { e.printStackTrace(); } } initTuli(); }else{//TypeIdField有值 List featureTypes= ((GeodatabaseFeatureTable)(((FeatureLayer)layer).getFeatureTable())).getFeatureTypes(); for(FeatureType featureType :featureTypes){ FeatureTemplate[] templates= featureType.getTemplates(); for(FeatureTemplate template :templates){ try { //根据FeatureTemplate生成GeodatabaseFeature GeodatabaseFeature feature= ((GeodatabaseFeatureTable)(((FeatureLayer)layer).getFeatureTable())).createFeatureWithTemplate(template, null); Renderer renderer = ((FeatureLayer) layer).getRenderer(); //获取符号 Symbol symbol = renderer.getSymbol(feature); //得到代表图例的图片 Bitmap bitmap=createBitmapFromSymbol(symbol,layer); String name=template.getName(); TuliBean bean=new TuliBean(name,bitmap); tuliBeanList.add(bean); //设置图例的布局和点击事件 //populateTemplateView(layerTemplate,bitmap,template,symbol,(FeatureLayer)layer); } catch (Exception e) { e.printStackTrace(); } } initTuli(); } } }
符号转换Bitmap的方法为:
//根据Symbol和Layer来创建相对应的图例 private Bitmap createBitmapFromSymbol(Symbol symbol,Layer layer) { Bitmap bitmap=null; Geometry.Type type= ((FeatureLayer)layer).getGeometryType(); //跟不同的Type来创建不同的图例 if(type.equals(Geometry.Type.POINT)){//点图层 Point point=new Point(20, 20); bitmap= SymbolHelper.getLegendImage(symbol, point, 50, 50, Color.WHITE); }else if(type.equals(Geometry.Type.POLYLINE)){//线图层 Polyline polyLine=new Polyline(); polyLine.startPath(0,0); polyLine.lineTo(40,40); bitmap=SymbolHelper.getLegendImage(symbol, polyLine, 50, 50, Color.WHITE); }else if(type.equals(Geometry.Type.POLYGON)){//面图层 Polygon polygon=new Polygon(); polygon.startPath(0, 0); polygon.lineTo(250, 0); polygon.lineTo(250, 100); polygon.lineTo(0, 100); polygon.lineTo(0, 0); bitmap= SymbolHelper.getLegendImage(symbol, polygon, 250, 100, Color.WHITE); } return bitmap; }
最后将获取到的图例展示出来:
baseRecyclerAdapterOfflineData=new BaseRecyclerAdapter(this,tuliBeanList) { @Override public int getItemLayoutId(int viewType) { return R.layout.item_tuli; } @Override public void bindData(RecyclerViewHolder holder, int position, TuliBean item) { TextView tvName=holder.getTextView(R.id.tv_tuliname); ImageView ivTuli=holder.getImageView(R.id.iv_tuli); tvName.setText(item.getTuliName()); ivTuli.setImageBitmap(item.getBitmap()); } }; rvDownload.setAdapter(baseRecyclerAdapterOfflineData); rvDownload.setLayoutManager(new LinearLayoutManager(this)); rvDownload.setItemAnimator(new DefaultItemAnimator());
看看最后的效果:
demo下载地址:http://download.csdn.net/detail/bit_kaki/9926236
参考文章:http://blog.csdn.net/yifei1989/article/details/17339095