ClassBreaksRenderer和ClassBreak用法

背景:新接触ArcGIS。看到以前的工程里用到了ClassBreaksRenderer和ClassBreak,网上查了下,还是没明白具体的用法。只知道大概是根据某个字段,对不同的值使用不同的显示方法。比如,温度的表示,用蓝色过渡到红色表示由低到高的温度。类似的还有海拔,人口密度等等。

然后写了个Demo测试了一下,整理了一下用法。


关系图:


图示说明:

  • Geometry: 点、线和多边形等都是Geometry。这是一个抽象类。
  • Symbol: 点,线等的显示方式。包括颜色,大小,图标等。这是一个接口。
  • Graphic:点、线等要显示在地图上,需要借助Graphic。创建Graphic的时候需要指定要显示的Geometry(Line、Point等)和对应的显示方式Symbol。
  • GraphicLayer: Graphic显示在GraphicLayer上。GraphicLayer可以添加多个Graphic对象。
  • MapView: GraphicLayer显示在MapView上。MapView可以添加多个Layer。调用一个MapView实例的addLayer方法,往MapViews中添加一个名为"graphicLayer"的GraphicLayer。
  • ClassBreaksRenderer: 每个GraphicsLayer都可以指定一个ClassBreaksRenderer。通过调用classBreaksRenderer.setField("index"),来指定根据"index"("index"可以换成任何字符串)的值用不同的显示方式。但它没说每个值的表示方式是什么。
  • ClassBreak:指出每个值的表示方式是什么。通过classBreak.setClassMaxValue(value);来设置"index"的值(这里没有指定字段名称)。通过classBreak.setSymbol(symbol)来指定对应的显示方式。ClassBreaksRenderer可以通过classBreaksRenderer.addClassBreak(classBreak);来添加多个ClassBreak。这样就有不同的值和对应的显示方式。
  • Attributs: 一个Map<String, Object>。ClassBreaksRenderer和ClassBreak指出了每个值对应的显示方式,但每个Graphic的这个字段的值怎么指定呢?这就用到这个Map了。创建Graphic的时候,有些构造器需要个一个Map,就是在这个Map中指定的。通过传入一个包含"index"的Map进来就好。如,
    [java] view plain copy
     print?
    1. Map<String, Object> attr = new HashMap<String, Object>();  
    2. attr.put("index"5);  
    3. Graphic graphic = new Graphic(point, null, attr, 0); //第二个参数Symbol一定要为null,不然ClassBreak指定的显示方式不管用。  

             应该说是这个属性Map把ClassBreaksRenderer和Graphic联系了起来,使得ClassBreak对Graphic生效。


代码:

代码主要集中在一个LayerUtil类中。

用法:

1. 在MapView中添加一个GraphicLayer。

2. 点击MapView的时候,在对应的监听器里调用markPoint来画点。

[java] view plain copy
 print?
  1. package com.chanryma.demo.common;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. import android.graphics.Color;  
  7. import android.util.Log;  
  8.   
  9. import com.esri.android.map.GraphicsLayer;  
  10. import com.esri.android.map.Layer;  
  11. import com.esri.android.map.MapView;  
  12. import com.esri.core.geometry.Point;  
  13. import com.esri.core.map.Graphic;  
  14. import com.esri.core.renderer.ClassBreak;  
  15. import com.esri.core.renderer.ClassBreaksRenderer;  
  16. import com.esri.core.symbol.SimpleMarkerSymbol;  
  17.   
  18. public class LayerUtil {  
  19.     private static ClassBreaksRenderer classBreaksRenderer;  
  20.     private static final int[] COLORS = { Color.BLACK, Color.BLUE, Color.CYAN, Color.DKGRAY, Color.GRAY, Color.GREEN, Color.LTGRAY };  
  21.   
  22.     static {  
  23.         initClassBreaksRenderer();  
  24.     }  
  25.   
  26.     /** 
  27.      *  在GraphicLayer上画点 
  28.      */  
  29.     public static void markPoint(GraphicsLayer layer, Point point, int index) {  
  30.         index %= COLORS.length;  
  31.   
  32.         Map<String, Object> attr = new HashMap<String, Object>();  
  33.         attr.put("index", index);  
  34.         Graphic graphic = new Graphic(point, null, attr, 0);  
  35.         layer.addGraphic(graphic);  
  36.     }  
  37.   
  38.     /** 
  39.      *  在MapView中添加一个GraphicLayer 
  40.      */  
  41.     public static void addGraphicsLayer(MapView mapView, String layerName) {  
  42.         GraphicsLayer graphicsLayer = new GraphicsLayer();  
  43.         graphicsLayer.setName(layerName);  
  44.         graphicsLayer.setRenderer(classBreaksRenderer);  
  45.         mapView.addLayer(graphicsLayer);  
  46.     }  
  47.   
  48.     /** 
  49.      *  根据名称取得GraphicLayer 
  50.      */  
  51.     public static GraphicsLayer getGraphicsLayer(MapView mapView, String layerName) {  
  52.         for (Layer layer : mapView.getLayers()) {  
  53.             if (layer instanceof GraphicsLayer) {  
  54.                 if (layer.getName().equals(layerName)) {  
  55.                     return (GraphicsLayer) layer;  
  56.                 }  
  57.             }  
  58.         }  
  59.   
  60.         return null;  
  61.     }  
  62.   
  63.     /** 
  64.      *  初始化ClassBreaksRenderer 
  65.      */  
  66.     private static void initClassBreaksRenderer() {  
  67.         if (classBreaksRenderer == null) {  
  68.             classBreaksRenderer = new ClassBreaksRenderer();  
  69.             classBreaksRenderer.setField("index");  
  70.             Log.v("LayerUtil""getMinValue() = " + classBreaksRenderer.getMinValue());  
  71.             for (int i = 0; i < COLORS.length; i++) {  
  72.                 ClassBreak classBreak = new ClassBreak();  
  73.                 classBreak.setClassMaxValue(i);  
  74.                 classBreak.setLabel("" + i);  
  75.                 classBreak.setSymbol(new SimpleMarkerSymbol(COLORS[i], 8, SimpleMarkerSymbol.STYLE.SQUARE));  
  76.                 classBreaksRenderer.addClassBreak(classBreak);  
  77.             }  
  78.         }  
  79.     }  
  80. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章