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进来就好。如,
    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<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来画点。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值