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://回退 drawTool.actionUndo(); break; case R.id.btnDrawDeleteNode://刪除節點 drawTool.actionDelete(); break; } } }