Android中ArcGIS中实现DrawTool类

package com.esri.drawtool;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.graphics.Color;
import android.view.MotionEvent;

import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.MapOnTouchListener;
import com.esri.android.map.MapView;
import com.esri.core.geometry.MultiPath;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.Polygon;
import com.esri.core.geometry.Polyline;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.SimpleFillSymbol;
import com.esri.core.symbol.SimpleLineSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol.STYLE;


public class DrawTool {
	
	//map属性
	private MapView map;
	//标绘图层
	private GraphicsLayer graphicsLayer;
	
	private MyTouchListener myTouch;
	
	
	//符号样式
	private SimpleMarkerSymbol sms;
	private SimpleLineSymbol sls;
	private SimpleFillSymbol sfs;
	
	public SimpleMarkerSymbol getSms() {
		return sms;
	}
	public void setSms(SimpleMarkerSymbol sms) {
		this.sms = sms;
	}
	public SimpleLineSymbol getSls() {
		return sls;
	}
	public void setSls(SimpleLineSymbol sls) {
		this.sls = sls;
	}
	public SimpleFillSymbol getSfs() {
		return sfs;
	}
	public void setSfs(SimpleFillSymbol sfs) {
		this.sfs = sfs;
	}

	

	//构造函数
	public DrawTool(Context context,MapView map,GraphicsLayer glayer) {
		
		this.map = map;
		this.graphicsLayer = glayer;
		//实例化监听器
		myTouch = new MyTouchListener(context, map,glayer);
		//设置绘画监听
		this.map.setOnTouchListener(myTouch);
		
	}
	//标绘点操作
	public void drawPoint(){
		this.myTouch.setType("POINT");
	}
	//标绘线操作
	public void drawLine(){
		this.myTouch.setType("POLYLINE");
	}
	//标绘面操作
	public void drawPolgyon(){
		this.myTouch.setType("POLYGON");
	}
	//取消标绘
	public void cancelDraw(){
		this.myTouch.setType("");
	}
	//清除图层信息	
	public void removeAll(){
		graphicsLayer.removeAll();
	}
	
	
	 /**
	  * 触摸监听类
	 * @author Administrator
	 *  
	 */
	class MyTouchListener extends MapOnTouchListener {

		MultiPath poly;
		String type = "";
		Point startPoint = null;
		int uid = 0;
		
		GraphicsLayer graphicsLayer;
		MapView mapView;

		public MyTouchListener(Context context, MapView view, GraphicsLayer glayer) {
			super(context, view);
			
			this.graphicsLayer = glayer;
			this.mapView = 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")) {
				
				if(sms == null)
					sms = new SimpleMarkerSymbol(Color.RED,25,STYLE.CIRCLE);
				Graphic graphic = new Graphic(mapView.toMapPoint(new Point(e.getX(), e
						.getY())),sms);
				//graphic.setGeometry();
				graphicsLayer.addGraphic(graphic);
				
				return true;
			}
			return false;

		}

		/**
		 * 点击地图并在屏幕上滑动的操作
		 *
		 */
		public boolean onDragPointerMove(MotionEvent from, MotionEvent to) {
			if (type.length() > 1
					&& (type.equalsIgnoreCase("POLYLINE") || type
							.equalsIgnoreCase("POLYGON"))) {

				Point mapPt = mapView.toMapPoint(to.getX(), to.getY());

				/*
				 *如果startPoint为空,者创建它并将设置为poly的起始点
				 */
				if (startPoint == null) {
					poly = type.equalsIgnoreCase("POLYLINE") ? new Polyline()
							: new Polygon();
					startPoint = mapView.toMapPoint(from.getX(), from.getY());
					poly.startPath((float) startPoint.getX(),
							(float) startPoint.getY());

					/*
					 * 创建也该Graphic对象,并将他添加到图层中去
					 */
					Graphic graphic;
					if(type.equalsIgnoreCase("POLYLINE")){
						//设置线型样式,如果为空者创建他
						Map  attutes = new HashMap<String, Object>();
						attutes.put("name", "tanghy");
						attutes.put("age", 11);
						if(sls == null)
							sls = new SimpleLineSymbol(Color.RED,2);
						 graphic = new Graphic(poly,sls,attutes,null);
					}else{
						if(sfs == null){
							//设置面状样式,如果为空者创建他
							sfs = new SimpleFillSymbol(Color.RED);
							sfs.setOutline(new SimpleLineSymbol(Color.BLUE,2));
							sfs.setAlpha(50);
						}
						 graphic = new Graphic(poly,sfs);
					}
					//建图形添加到图层中
					uid = graphicsLayer.addGraphic(graphic);
				}

				poly.lineTo((float) mapPt.getX(), (float) mapPt.getY());
				//更新图形
				graphicsLayer.updateGraphic(uid, poly);
				
				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"))) {

				/*
				 * 判断当是面状要素时添加第一个点到poly上.
				 */
				if (type.equalsIgnoreCase("POLYGON")) {
					poly.lineTo((float) startPoint.getX(),
							(float) startPoint.getY());
					
				}
				startPoint = null;
				return true;
			}
			return super.onDragPointerUp(from, to);
		}

	}
	
	
	

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值