背景:新接触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); //第二个参数Symbol一定要为null,不然ClassBreak指定的显示方式不管用。
Map<String, Object> attr = new HashMap<String, Object>(); attr.put("index", 5); Graphic graphic = new Graphic(point, null, attr, 0); //第二个参数Symbol一定要为null,不然ClassBreak指定的显示方式不管用。
应该说是这个属性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();
- }
- /**
- * 在GraphicLayer上画点
- */
- 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);
- }
- /**
- * 在MapView中添加一个GraphicLayer
- */
- public static void addGraphicsLayer(MapView mapView, String layerName) {
- GraphicsLayer graphicsLayer = new GraphicsLayer();
- graphicsLayer.setName(layerName);
- graphicsLayer.setRenderer(classBreaksRenderer);
- mapView.addLayer(graphicsLayer);
- }
- /**
- * 根据名称取得GraphicLayer
- */
- 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;
- }
- /**
- * 初始化ClassBreaksRenderer
- */
- 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);
- }
- }
- }
- }
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();
}
/**
* 在GraphicLayer上画点
*/
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);
}
/**
* 在MapView中添加一个GraphicLayer
*/
public static void addGraphicsLayer(MapView mapView, String layerName) {
GraphicsLayer graphicsLayer = new GraphicsLayer();
graphicsLayer.setName(layerName);
graphicsLayer.setRenderer(classBreaksRenderer);
mapView.addLayer(graphicsLayer);
}
/**
* 根据名称取得GraphicLayer
*/
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;
}
/**
* 初始化ClassBreaksRenderer
*/
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);
}
}
}
}
版权声明:转载请注明出处http://blog.csdn.net/chanryma