packagecom.gis_luq.drawtoolsdemo;importandroid.app.Activity;importandroid.content.Context;importandroid.os.Bundle;importandroid.widget.Button;importcom.esri.android.map.GraphicsLayer;importcom.esri.android.map.MapOnTouchListener;importcom.esri.android.map.MapView;importcom.esri.android.map.ags.ArcGISTiledMapServiceLayer;importcom.esri.core.map.Graphic;importcom.esri.core.table.TableException;importcom.gis_luq.lib.Draw.DrawEvent;importcom.gis_luq.lib.Draw.DrawEventListener;importcom.gis_luq.lib.Draw.DrawTool;importjava.io.FileNotFoundException;public class MainActivity extends Activity implementsDrawEventListener {privateContext context;private MapView mapView = null;private ArcGISTiledMapServiceLayer arcGISTiledMapServiceLayer = null;private GraphicsLayer graphicsLayer = null;private Graphic selectGraphic = null;privateDrawTool drawTool;public MapOnTouchListener mapDefaultOnTouchListener;//默认点击事件
public DrawEventListener drawEventListener;//要素绘制点击事件
@Overrideprotected voidonCreate(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 = newArcGISTiledMapServiceLayer(strMapUrl);this.mapView.addLayer(arcGISTiledMapServiceLayer);
graphicsLayer= newGraphicsLayer();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= newToolsOnClickListener(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);
}
@Overridepublic void handleDrawEvent(DrawEvent event) throwsTableException, FileNotFoundException {//将画好的图形(已经实例化了Graphic),添加到drawLayer中并刷新显示
this.graphicsLayer.addGraphic(event.getDrawGraphic());//修改点击事件为默认
this.mapView.setOnTouchListener(mapDefaultOnTouchListener);
}
}