標籤:
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
使用流程
- 初始化DrawTool工具。
- 使用Activity擴充DrawEventListener ,並將當前Activity設定為DrawTool的Listener。
- 實現DrawEventListener中的handleDrawEvent方法。
- 使用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://回退 if (drawTool.isActive()){ drawTool.actionUndo(); } break; case R.id.btnDrawDeleteNode://刪除節點 if (drawTool.isActive()){ drawTool.actionDelete(); } break; } }}
《ArcGIS Runtime SDK for Android開發筆記》——(15)、要素繪製Drawtools3.0工具DEMO