《ArcGIS Runtime SDK for Android開發筆記》——(15)、要素繪製Drawtools3.0工具DEMO

一、前言

移動GIS項目開發中點線面的要素繪製及編輯是最經常使用的操做,在ArcGIS Runtime SDK for iOS 自帶AGSSketchLayer類能夠幫助用戶快速實現要素的繪製,圖形編輯。可是在ArcGIS Runtime SDK for Android的版本中並無提供相似的功能,實現過程相對較複雜。(10.2.8及如下版本須要用戶自定義擴展實現,經過擴展MapOnTouchListener類實現,Quartz版SDK默認自帶)html

以前有大神gispace封裝了DrawTools2.0工具類DEMO,實現了簡單的要素繪製。可是並無對要素繪製及編輯狀態作很好的體現,如節點,回退操做等。因此鑑於此,我在DrawTools2.0工具類基礎上擴展實現了DrawTools3.0,該版本可以實現基本點線面要素的繪製,精細化展示節點變化信息,支持加點,刪點,移點操做。java

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

轉載請註明出處:http://www.cnblogs.com/gis-luq/p/5857661.html git

二、使用說明

DrawTools3.0基於DrawTools2.0擴展開發而來,使用思路基本一致,增長節點增長、節點刪除、回退操做,要素編輯狀態開啓與關閉操做。app

開源項目庫地址:http://git.oschina.net/gis-luq/DrawTool3.0ide

使用流程工具

  1. 初始化DrawTool工具。
  2. 使用Activity擴展DrawEventListener ,並將當前Activity設置爲DrawTool的Listener。
  3. 實現DrawEventListener中的handleDrawEvent方法。
  4. 使用DrawTool工具繪製圖形。

MainActivity.java this

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

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

    }
}
相關文章
相關標籤/搜索