《ArcGIS Runtime SDK for Android开发笔记》——(15)、要素绘制Drawtools3.0工具DEMO

1、前言

移动GIS项目开发中点线面的要素绘制及编辑是最常用的操作,在ArcGIS Runtime SDK for iOS 自带AGSSketchLayer类可以帮助用户快速实现要素的绘制,图形编辑。但是在ArcGIS Runtime SDK for Android的版本中并没有提供类似的功能,实现过程相对较复杂。(10.2.8及以下版本需要用户自定义扩展实现,通过扩展MapOnTouchListener类实现,Quartz版SDK默认自带)

之前有大神gispace封装了DrawTools2.0工具类DEMO,实现了简单的要素绘制。但是并没有对要素绘制及编辑状态做很好的体现,如节点,回退操作等。所以鉴于此,我在DrawTools2.0工具类基础上扩展实现了DrawTools3.0,该版本能够实现基本点线面要素的绘制,精细化展现节点变化信息,支持加点,删点,移点操作。

DrawTools2.0地址:http://blog.csdn.net/gispace/article/details/6723459

转载请注明出处:http://www.cnblogs.com/gis-luq/p/5857661.html 

2、使用说明

DrawTools3.0基于DrawTools2.0扩展开发而来,使用思路基本一致,增加节点增加、节点删除、回退操作,要素编辑状态开启与关闭操作。

开源项目库地址:http://git.oschina.net/gis-luq/DrawTool3.0

使用流程

  1. 初始化DrawTool工具。
  2. 使用Activity扩展DrawEventListener ,并将当前Activity设置为DrawTool的Listener。
  3. 实现DrawEventListener中的handleDrawEvent方法。
  4. 使用DrawTool工具绘制图形。

MainActivity.java 

复制代码
package com.gis_luq.drawtoolsdemo;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

import android.widget.Button;

import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.MapOnTouchListener;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;
import com.esri.core.map.Graphic;
import com.esri.core.table.TableException;
import com.gis_luq.lib.Draw.DrawEvent;
import com.gis_luq.lib.Draw.DrawEventListener;
import com.gis_luq.lib.Draw.DrawTool;

import java.io.FileNotFoundException;

public class MainActivity extends Activity implements DrawEventListener {

    private Context context;
    private MapView mapView = null;
    private ArcGISTiledMapServiceLayer arcGISTiledMapServiceLayer = null;
    private GraphicsLayer graphicsLayer = null;

    private Graphic selectGraphic = null;
    private DrawTool drawTool;

    public MapOnTouchListener mapDefaultOnTouchListener;//默认点击事件
    public DrawEventListener drawEventListener;//要素绘制点击事件


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context = this;

        this.mapView = (MapView)this.findViewById(R.id.map);//设置UI和代码绑定

        String strMapUrl="http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer";
        this.arcGISTiledMapServiceLayer = new ArcGISTiledMapServiceLayer(strMapUrl);
        this.mapView.addLayer(arcGISTiledMapServiceLayer);

        graphicsLayer = new GraphicsLayer();
        this.mapView.addLayer(graphicsLayer);

        // 初始化DrawTool实例
        this.drawTool = new DrawTool(this.mapView);
        // 将本Activity设置为DrawTool实例的Listener
        this.drawTool.addEventListener(this);

        //设置地图事件
        mapDefaultOnTouchListener = new MapOnTouchListener(this.mapView.getContext(), this.mapView);
        drawEventListener = this;

        ToolsOnClickListener toolsOnClickListener = new ToolsOnClickListener(context,drawTool,selectGraphic,mapView);
        Button btnDrawPoint = (Button)this.findViewById(R.id.btnDrawPoint);
        btnDrawPoint.setOnClickListener(toolsOnClickListener);

        Button btnDrawPolyline = (Button)this.findViewById(R.id.btnDrawPolyline);
        btnDrawPolyline.setOnClickListener(toolsOnClickListener);

        Button btnDrawFreePolyline = (Button)this.findViewById(R.id.btnDrawFreePolyline);
        btnDrawFreePolyline.setOnClickListener(toolsOnClickListener);

        Button btnDrawPolygon = (Button)this.findViewById(R.id.btnDrawPolygon);
        btnDrawPolygon.setOnClickListener(toolsOnClickListener);

        Button btnDrawFreePolygon = (Button)this.findViewById(R.id.btnDrawFreePolygon);
        btnDrawFreePolygon.setOnClickListener(toolsOnClickListener);

        Button btnDrawCircle = (Button)this.findViewById(R.id.btnDrawCircle);
        btnDrawCircle.setOnClickListener(toolsOnClickListener);

        Button btnDrawEnvlope = (Button)this.findViewById(R.id.btnDrawEnvlope);
        btnDrawEnvlope.setOnClickListener(toolsOnClickListener);

        Button btnDrawEditor = (Button)this.findViewById(R.id.btnDrawSave);
        btnDrawEditor.setOnClickListener(toolsOnClickListener);

        Button btnDrawUndo = (Button)this.findViewById(R.id.btnDrawUndo);
        btnDrawUndo.setOnClickListener(toolsOnClickListener);

        Button btnDrawDeleteNode = (Button)this.findViewById(R.id.btnDrawDeleteNode);
        btnDrawDeleteNode.setOnClickListener(toolsOnClickListener);

    }

    @Override
    public void handleDrawEvent(DrawEvent event) throws TableException, FileNotFoundException {
        // 将画好的图形(已经实例化了Graphic),添加到drawLayer中并刷新显示
        this.graphicsLayer.addGraphic(event.getDrawGraphic());
        // 修改点击事件为默认
        this.mapView.setOnTouchListener(mapDefaultOnTouchListener);
    }
}
复制代码

 ToolsOnClickListener.java

复制代码
package com.gis_luq.drawtoolsdemo;

import android.content.Context;
import android.view.View;

import com.esri.android.map.MapView;
import com.esri.core.map.Graphic;
import com.gis_luq.lib.Draw.DrawTool;

/**
 * 绘图点击事件
 * Created by gis-luq on 2016/1/2.
 */
public class ToolsOnClickListener implements View.OnClickListener {

    private Context context = null;
    private DrawTool drawTool = null;
    private Graphic selectGraphic =null;
    private MapView mapView = null;

    public ToolsOnClickListener(Context context, DrawTool drawTool, Graphic selectGraphic, MapView mapView) {
        this.context = context;
        this.drawTool = drawTool;
        this.selectGraphic = selectGraphic;
        this.mapView = mapView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnDrawPoint://绘制点
                drawTool.activate(DrawTool.POINT);
                break;
            case R.id.btnDrawPolyline://绘制线
                drawTool.activate(DrawTool.POLYLINE);
                break;
            case R.id.btnDrawFreePolyline://绘制流状线
                drawTool.activate(DrawTool.FREEHAND_POLYLINE);
                break;
            case R.id.btnDrawPolygon://绘制面
                drawTool.activate(DrawTool.POLYGON);
                break;
            case R.id.btnDrawFreePolygon://绘制流状面
                drawTool.activate(DrawTool.FREEHAND_POLYGON);
                break;
            case R.id.btnDrawCircle://绘制圆
                drawTool.activate(DrawTool.CIRCLE);
                break;
            case R.id.btnDrawEnvlope://绘制矩形
                drawTool.activate(DrawTool.ENVELOPE);
                break;
            case R.id.btnDrawSave://保存
                drawTool.sendDrawEndEvent();
                break;
            case R.id.btnDrawUndo://回退
                drawTool.actionUndo();
                break;
            case R.id.btnDrawDeleteNode://删除节点
                drawTool.actionDelete();
                break;
        }

    }
}
复制代码

 

文章若无特殊说明均为原创,原创作品,允许转载,转载时请务必以超链接形式标明文章出处、作者信息和本声明。
博客:http://www.cnblogs.com/gis-luq​ 作者:gis-luq 邮箱:luqiang.gis@foxmail.com

转载于:https://www.cnblogs.com/telwanggs/p/6434340.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值