为了账号安全,请及时绑定邮箱和手机立即绑定

ArcGIS Runtime For Android 开发(4)

标签:
Android
第四课 图形绘制

加载完地图以后,我们可能需要做一些数据采集的操作,那么绘制图形就非常的有用处了。在ArcGIS Runtime For Android 中,我们使用GraphicsLayer来承载绘制的图形。

我们先看一下绘制的逻辑:图片描述
地图中应该包含绘制层---------->GraphicsLayer
在界面上,点击按钮向系统发送命令-------->需要一个枚举类,存放命令
地图需要监听点击事件 ---------->OnSingleTapListener
每个图形需要一个默认的符号------>Symbol
图形是根据点来绘制的,所以需要保存图形的点数据------->List<Point>
那么接下来就简单了,根据ArcGIS Runtime For Android 提供的API,我们就可以完成图形的绘制。
首先,新建一个ActionCommand枚举类

public enum ActionCommand {
     NULL,                 //置空
    DRAW_POINT,           //绘制点
    DRAW_LINE,            //绘制线
    DRAW_POLYGON        //绘制面
}

在地图界面,实例化一个GraphicsLayer,并将这个GraphicsLayer加入到Map中;

GraphicsLayer drawLayer;
MapView mapView;
List<Point> tempDrawPoints;
PloyLine pPloyLine;
Ploygon pPloygon;
……
public void InitControl()
{
     mapView =(MapView) findViewById(R.id.mapView);
     mapView.setOnSingleTapListener(onSingleTap);
}
public InitData()
{
    String url ="https://services.arcgisonline.com/arcgis/rest/services/World_Imagery/MapServer"; 
    ArcGISTiledMapServiceLayer serviceLayer=new ArcGISTiledMapServiceLayer(url ); 
    mapView.addLayer(serviceLayer);//添加图层到地图窗口中
    drawLayer=new GraphicsLayer ();
    mapView.addLayer(drawLayer);
   tempDrawPoints=new ArrayList();
}

添加监听事件:

 OnSingleTapListener onSingleTapListener = new OnSingleTapListener() {

        @Override
        public void onSingleTap(float v, float v1) {
            Point pnt = mapView.toMapPoint(v, v1);
            switch (SystemManager.curAction) {
                case NULL: 
                    break;  
                case DRAW_POINT:
                    DrawPoint(pnt );
                    break;
                case DRAW_LINE: 
                    DrawLine(pnt );
                    break;
                case DRAW_POLYGON:  
                    DrawPolygon(pnt );
                    break; 
            }
        }
    };

然后是绘制图形:

public void DrawPoint(Point pnt)
{
       Graphic graphic = new Graphic(curPoint, simpleSymbol);
       drawLayer.addGraphic(graphic);
}
public void DrawLine(Point pnt)
{
    if (tempPoints.size() == 1) {
    Point point = tempDrawPoints.get(0);
    DrawPoint(point )
} 
esle{
    drawLayer.removeAll();
    pPloyLine= new PloyLine();
    for (int i = 1; i < tempDrawPoints.size(); i++) {
        Point sPoint = tempDrawPoints.get(i - 1);
        Point ePoint = tempDrawPoints.get(i);
        Line line = new Line();
        line.setStart(sPoint);
        line.setEnd(ePoint);
        pPloyLine.addSegment(line, false);
    }
    Graphic polylineGraphic= new Graphic(pPloyLine,
                    MapManager.simpleLineSymbol);
    int UID=drawLayer.addGraphic(polylineGraphic);}
}
public void DrawPolygon()
{
    if (tempPoints.size() == 1) {
    Point point = tempDrawPoints.get(0);
     DrawPoint(point )
    } 
   else {
            drawLayer.removeAll();
            pPolygon = new Polygon();
            for (int i = 1; i < tempDrawPoints.size(); i++) {
                Point sPoint = tempDrawPoints.get(i - 1);
                Point ePoint = tempDrawPoints.get(i);
                Line line = new Line();
                line.setStart(sPoint);
                line.setEnd(ePoint);
                pPolygon.addSegment(line, false);
            }
            Graphic polygonGraphic = new Graphic(pPolygon,
                    MapManager.simpleFillSymbolInspection);
            int UID=drawLayer.addGraphic(polygonGraphic);
        }
} 
点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消