几何对象的绘制



public class DrawGraphicElements extends Activity implements OnClickListener {

	MapView mapView = null;
	ArcGISTiledMapServiceLayer mTileMapLayer = null;
	GraphicsLayer graphicsLayer = null;
	MyTouchListener mTouchListener = null;
	Button geometryButton = null;
	Button clearButton = null;
	String mapURL = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/PublicSafety/PublicSafetyBasemap/MapServer";
	final String[] geometryTypes = new String[] { "绘制点", "绘制线", "绘制区域" };
	int selectedGeometryIndex = -1;

	@SuppressWarnings("serial")
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mapView = (MapView) findViewById(R.id.map);
		mTouchListener = new MyTouchListener(DrawGraphicElements.this, mapView);
		mapView.setOnTouchListener(mTouchListener);

		geometryButton = (Button) findViewById(R.id.geometrybutton);
		geometryButton.setEnabled(false);
		geometryButton.setOnClickListener(this);

		clearButton = (Button) findViewById(R.id.clearbutton);
		clearButton.setOnClickListener(this);

		mTileMapLayer = new ArcGISTiledMapServiceLayer(mapURL);
		graphicsLayer = new GraphicsLayer();
		mapView.addLayer(mTileMapLayer);
		mapView.addLayer(graphicsLayer);// 添加客户端要素图层

		// 地图加载的时候被调用:检查地图是否可用
		mTileMapLayer.setOnStatusChangedListener(new OnStatusChangedListener() {
			public void onStatusChanged(Object arg0, STATUS status) {
				if (status.equals(OnStatusChangedListener.STATUS.INITIALIZED)) {
					// 可用绘制按钮可用
					geometryButton.setEnabled(true);
				}
			}
		});

	}

	// 地图触摸事件
	class MyTouchListener extends MapOnTouchListener {

		MultiPath mMutiPath;//多路径
		String type = "";
		Point startPoint = null;

		public MyTouchListener(Context context, MapView view) {
			super(context, view);
		}

		public void setType(String geometryType) {
			this.type = geometryType;
		}

		public String getType() {
			return this.type;
		}

		// 绘制一个点
		public boolean onSingleTap(MotionEvent e) {
			if (type.length() > 1 && type.equalsIgnoreCase("POINT")) {
				graphicsLayer.removeAll();//清空要素图层

				Point point = mapView.toMapPoint(new Point(e.getX(), e.getY()));
				// 注意:因手机分辨率的差异,像素点的大小是有差别的
				SimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbol(
						Color.BLUE, 30, STYLE.CIRCLE);
				Graphic graphic = new Graphic(point, markerSymbol);
				graphicsLayer.addGraphic(graphic);

				clearButton.setEnabled(true);
				return true;
			}
			return false;

		}

		//绘制一个点并移动
		public boolean onDragPointerMove(MotionEvent from, MotionEvent to) {
			if (type.length() > 1
					&& (type.equalsIgnoreCase("POLYLINE") || type
							.equalsIgnoreCase("POLYGON"))) {

				//终点
				Point toPoint = mapView.toMapPoint(to.getX(), to.getY());

				if (startPoint == null) {
					graphicsLayer.removeAll();
					//绘制线还是绘制区域
					mMutiPath = type.equalsIgnoreCase("POLYLINE") ? new Polyline(): new Polygon();
					//记录开始点
					startPoint = mapView.toMapPoint(from.getX(), from.getY());
					mMutiPath.startPath((float) startPoint.getX(),(float) startPoint.getY());

					SimpleLineSymbol lineSymbol=new SimpleLineSymbol(Color.BLUE,5);
					Graphic graphic = new Graphic(startPoint,lineSymbol);
					graphicsLayer.addGraphic(graphic);
				}

				mMutiPath.lineTo((float) toPoint.getX(), (float) toPoint.getY());

				return true;
			}
			return super.onDragPointerMove(from, to);

		}

		//绘制一个点后抬起
		@Override
		public boolean onDragPointerUp(MotionEvent from, MotionEvent to) {
			if (type.length() > 1
					&& (type.equalsIgnoreCase("POLYLINE") || type
							.equalsIgnoreCase("POLYGON"))) {
				if (type.equalsIgnoreCase("POLYGON")) {
					mMutiPath.lineTo((float) startPoint.getX(),(float) startPoint.getY());
					graphicsLayer.removeAll();
					//添加面
					Graphic graphic=new Graphic(mMutiPath,new SimpleFillSymbol(Color.WHITE)); 
					graphicsLayer.addGraphic(graphic);
					startPoint = null;
					clearButton.setEnabled(true);
				}
				else{
					//添加线
					Graphic graphic=new Graphic(mMutiPath,new SimpleLineSymbol(Color.BLUE, 5));
					graphicsLayer.addGraphic(graphic);
					startPoint = null;
					clearButton.setEnabled(true);
				}
				return true;
			}
			return super.onDragPointerUp(from, to);
		}
	}

	// 对话框
	protected Dialog onCreateDialog(int id) {
		return new AlertDialog.Builder(DrawGraphicElements.this).setTitle("绘制")
				.setItems(geometryTypes, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						graphicsLayer.removeAll();
						String geomType = geometryTypes[which];
						selectedGeometryIndex = which;
						if (geomType.equalsIgnoreCase(geometryTypes[2])) {
							mTouchListener.setType("POLYGON");
						} else if (geomType.equalsIgnoreCase(geometryTypes[1])) {
							mTouchListener.setType("POLYLINE");
						} else if (geomType.equalsIgnoreCase(geometryTypes[0])) {
							mTouchListener.setType("POINT");
						}
					}
				}).create();
	}

	@Override
	protected void onPause() {
		super.onPause();
		mapView.pause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		mapView.unpause();
	}

	@Override
	public void onClick(View v) {
		if (v == geometryButton) {
			showDialog(0);
		} else if (v == clearButton) {
			graphicsLayer.removeAll();
			clearButton.setEnabled(false);
		}

	}

}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值