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. }  

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