ArcGIS for Android入門程式之DrawTool2.0

arcgis_mobile發表於2012-10-18

GISpace部落格《ArcGIS for Android入門程式之DrawTool》http://blog.csdn.net/gispace/article/details/6723459 在ArcGIS Android SDK 0.9版本實現繪製各種幾個圖形。ArcGIS Android SDK目前版本為2.0,較之前版本變化較大,故將之前版本移植到2.0版本下。原始碼下載地址http://download.csdn.net/detail/arcgis_mobile/4659389

該程式主要說明如何處理與MapView互動的各種事件,以訂閱釋出模式封裝幾何圖形繪製工具類DrawTool,使用方法如下:


package cn.com.esrichina.drawtool;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;

public class DrawToolActivity extends Activity implements DrawEventListener {

	private MapView mapView;
	private GraphicsLayer drawLayer;
	private DrawTool drawTool;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		this.mapView = (MapView) this.findViewById(R.id.map);
		// 新增底圖
		this.mapView
				.addLayer(new ArcGISTiledMapServiceLayer(
						"http://www.arcgisonline.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer"));
		// 在drawLayer上繪製幾何圖形
		this.drawLayer = new GraphicsLayer();
		this.mapView.addLayer(this.drawLayer);
		this.drawTool = new DrawTool(this.mapView);
		// 此類實現DawEventListener介面
		this.drawTool.addEventListener(this);
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = this.getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.point:
			drawTool.activate(DrawTool.POINT);
			break;
		case R.id.envelope:
			drawTool.activate(DrawTool.ENVELOPE);
			break;
		case R.id.polygon:
			drawTool.activate(DrawTool.POLYGON);
			break;
		case R.id.polyline:
			drawTool.activate(DrawTool.POLYLINE);
			break;
		case R.id.freehandpolygon:
			drawTool.activate(DrawTool.FREEHAND_POLYGON);
			break;
		case R.id.freehandpolyline:
			drawTool.activate(DrawTool.FREEHAND_POLYLINE);
			break;
		case R.id.circle:
			drawTool.activate(DrawTool.CIRCLE);
			break;
		case R.id.clear:
			this.drawLayer.removeAll();
			this.drawTool.deactivate();
			break;
		}
		return true;
	}

	// 實現DrawEventListener中定義的方法
	public void handleDrawEvent(DrawEvent event) {
		// 將畫好的圖形(已經例項化了Graphic),新增到drawLayer中並重新整理顯示
		this.drawLayer.addGraphic(event.getDrawGraphic());
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}

在Android模擬器執行效果圖如下:



相關文章