背景:新接觸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進來就好。如,
-
Map<String, Object> attr = new HashMap<String, Object>();
-
attr.put("index", 5);
-
Graphic graphic = new Graphic(point, null, attr, 0);
應該說是這個屬性Map把ClassBreaksRenderer和Graphic聯繫了起來,使得ClassBreak對Graphic生效。
代碼:
代碼主要集中在一個LayerUtil類中。
用法:
1. 在MapView中添加一個GraphicLayer。
2. 點擊MapView的時候,在對應的監聽器裏調用markPoint來畫點。
-
package com.chanryma.demo.common;
-
-
import java.util.HashMap;
-
import java.util.Map;
-
-
import android.graphics.Color;
-
import android.util.Log;
-
-
import com.esri.android.map.GraphicsLayer;
-
import com.esri.android.map.Layer;
-
import com.esri.android.map.MapView;
-
import com.esri.core.geometry.Point;
-
import com.esri.core.map.Graphic;
-
import com.esri.core.renderer.ClassBreak;
-
import com.esri.core.renderer.ClassBreaksRenderer;
-
import com.esri.core.symbol.SimpleMarkerSymbol;
-
-
public class LayerUtil {
-
private static ClassBreaksRenderer classBreaksRenderer;
-
private static final int[] COLORS = { Color.BLACK, Color.BLUE, Color.CYAN, Color.DKGRAY, Color.GRAY, Color.GREEN, Color.LTGRAY };
-
-
static {
-
initClassBreaksRenderer();
-
}
-
-
-
-
-
public static void markPoint(GraphicsLayer layer, Point point, int index) {
-
index %= COLORS.length;
-
-
Map<String, Object> attr = new HashMap<String, Object>();
-
attr.put("index", index);
-
Graphic graphic = new Graphic(point, null, attr, 0);
-
layer.addGraphic(graphic);
-
}
-
-
-
-
-
public static void addGraphicsLayer(MapView mapView, String layerName) {
-
GraphicsLayer graphicsLayer = new GraphicsLayer();
-
graphicsLayer.setName(layerName);
-
graphicsLayer.setRenderer(classBreaksRenderer);
-
mapView.addLayer(graphicsLayer);
-
}
-
-
-
-
-
public static GraphicsLayer getGraphicsLayer(MapView mapView, String layerName) {
-
for (Layer layer : mapView.getLayers()) {
-
if (layer instanceof GraphicsLayer) {
-
if (layer.getName().equals(layerName)) {
-
return (GraphicsLayer) layer;
-
}
-
}
-
}
-
-
return null;
-
}
-
-
-
-
-
private static void initClassBreaksRenderer() {
-
if (classBreaksRenderer == null) {
-
classBreaksRenderer = new ClassBreaksRenderer();
-
classBreaksRenderer.setField("index");
-
Log.v("LayerUtil", "getMinValue() = " + classBreaksRenderer.getMinValue());
-
for (int i = 0; i < COLORS.length; i++) {
-
ClassBreak classBreak = new ClassBreak();
-
classBreak.setClassMaxValue(i);
-
classBreak.setLabel("" + i);
-
classBreak.setSymbol(new SimpleMarkerSymbol(COLORS[i], 8, SimpleMarkerSymbol.STYLE.SQUARE));
-
classBreaksRenderer.addClassBreak(classBreak);
-
}
-
}
-
}
-
}