🔍
📢

Arcgis for android绘图画点、线、面

一:实现绘图功能的思路

1:首先需要一个点击地图的一个监听函数,可以实现点击地图画点、线、面。arcgis 提供一个MapOnTouchListener类。

2:创建点、线、面对象,arcgis提供的类有Point,Polygon,Line

3:设置样式,不管是点、线、面都有自己的样式。arcgis提供了 SimpleLineSymbol (线样式类)SimpleMarkerSymbol(点样式类)  SimpleFillSymbol(面样式类)

4:创建一个绘画的图层,GraphicsLayer

5:将绘画图层添加到地图上。

二:实现代码

1:创建一个类继承MapOnTouchListener用来监听地图的点击 

 
  1. public class MapTouchListener extends MapOnTouchListener {  
  2.      /**  
  3.      * 单击地图  
  4.      */  
  5.     public boolean onSingleTap(MotionEvent point) {  
  6.         }  

2:创建点、线、面对象

  1. //屏幕坐标转化成空间坐标
  2. Point mapPoint = map.toMapPoint(point.getX(),point.getY());

3:创建样式

  1. //设置点、线、面的样式
  2. private void initSymbols(){
  3. markerSymbol = new SimpleMarkerSymbol(Color.BLUE,10,STYLE.CIRCLE);
  4. lineSymbol = new SimpleLineSymbol(Color.BLACK, 1, SimpleLineSymbol.STYLE.SOLID);
  5. fillSymbol = new SimpleFillSymbol(Color.BLACK, SimpleFillSymbol.STYLE.SOLID);
  6. fillSymbol.setAlpha(33);//设置的透明度
  7. }

4:创建一个绘画图层

  1. //创建绘图图层 对象
  2. drawLayer = new GraphicsLayer();
  3. mMapView.addLayer(drawLayer);

5:添加到地图上(这一定要加上,否则地图的监听事件无法实现)

  1. //绑定触摸事件监听器
  2. mapTouchListener=new MapTouchListener(EarthquakeActivity.this,mMapView);
  3. mapTouchListener.setLayer(drawLayer);
  4. mMapView.setOnTouchListener(mapTouchListener);

6:完整代码

package cn.zzu.Graphic;
 
import java.text.DecimalFormat;
import java.util.ArrayList;
 
import android.content.Context;
import android.graphics.Color;
import android.view.MotionEvent;
import android.widget.Toast;
 
import cn.zzu.Global.Variable;
import cn.zzu.Query.MyIdentifyTask;
 
import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.MapOnTouchListener;
import com.esri.android.map.MapView;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.Geometry.Type;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.Line;
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.geometry.SpatialReference;
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;
import com.esri.core.tasks.identify.IdentifyParameters;
 
public class MapTouchListener extends MapOnTouchListener {
	private Context context;//上下文
	private MapView map;//地图对象
	private GraphicsLayer graphicsLayer;//画图图层
	private Geometry.Type geoType = null;//绘图的对象
	private Point endPonit = null;
	private Polygon polygon;
	private Polygon webPolygon;
	private SimpleLineSymbol lineSymbol;  
	private SimpleMarkerSymbol markerSymbol;  
	private SimpleFillSymbol fillSymbol;
	private ArrayList points=null;//记录全部点
	private IdentifyParameters params;
	public MapTouchListener(Context context, MapView map) {		
		super(context, map);
		this.context = context;
		this.map = map;
		//样式初始化
		initSymbols();
	}
	
	// 根据用户选择设置当前绘制的几何图形类型
	public void setDrawType(Geometry.Type geotype)
	{
		this.geoType=geotype;
		//将上一次绘图的图形删除
		graphicsLayer.removeAll();
		endPonit = null;
		if(geoType == Geometry.Type.POLYGON)
			points=new ArrayList();
	}
	/**
	 * 判断是否量测
	 * @param measure
	 */
	public void setMeasure(boolean measure){
		if(geoType == Geometry.Type.POLYLINE)
			Variable.measureLength = measure;
		if(geoType == Geometry.Type.POLYGON)
			Variable.measureArea = measure;
	}
	/**
	 * 
	 * 创建画图图层
	 * @param drawLayer
	 */
	public void setLayer(GraphicsLayer drawLayer){
		this.graphicsLayer=drawLayer;
	}
	//设置点、线、面的样式
    private void initSymbols(){
    	markerSymbol = new SimpleMarkerSymbol(Color.BLUE,10,STYLE.CIRCLE);
    	lineSymbol =  new SimpleLineSymbol(Color.BLACK, 1, SimpleLineSymbol.STYLE.SOLID);
    	fillSymbol = new SimpleFillSymbol(Color.BLACK, SimpleFillSymbol.STYLE.SOLID);
    	fillSymbol.setAlpha(33);//设置的透明度
    } 
    
    public void setQueryParams(){
    	//实例化对象,并且给实现初始化相应的值
    	params = new IdentifyParameters();//创建查询的对象
    	params.setTolerance(20);//设置识别的容差
    	params.setDPI(98);//设置自分辨率
    	params.setLayers(new int[]{0,1,2,3,6});//设置识别的图层
    	params.setLayerMode(IdentifyParameters.ALL_LAYERS);//设置模式为识别服务上所有的图层
    }
 
	/**
	 * 单击地图
	 */
	public boolean onSingleTap(MotionEvent point) {
		//屏幕坐标转化成空间坐标
		Point mapPoint = map.toMapPoint(point.getX(),point.getY());
		if(Variable.singleQuery&&geoType ==null){
			params.setGeometry(mapPoint);
		    params.setSpatialReference(map.getSpatialReference());    // 设置坐标系                                         
		    params.setMapHeight(map.getHeight());
		    params.setMapWidth(map.getWidth());
		    Envelope env = new Envelope();
		    map.getExtent().queryEnvelope(env);
		    params.setMapExtent(env);
		      //我们自己扩展的异步类
		    MyIdentifyTask mTask = new MyIdentifyTask(context,map,mapPoint);
		    mTask.execute(params);//执行异步操作并传递所需的参数    
		}
		
		if(geoType == Geometry.Type.POLYGON)
			points.add(mapPoint);//将当前点加入点集合中
		if(geoType == null)
			return true;
		if(geoType == Geometry.Type.POINT){
			//画点
			Graphic graphic = new Graphic(mapPoint,markerSymbol);
			graphicsLayer.addGraphic(graphic);
		}else{
			if(endPonit==null){
				//线或者面的第一个点
				Graphic graphic = new Graphic(mapPoint,markerSymbol);
				graphicsLayer.addGraphic(graphic);
			}else{
				//画点
				Graphic graphic = new Graphic(mapPoint,markerSymbol);
				graphicsLayer.addGraphic(graphic);
				//两点连线
				Line line = new Line() ;
				line.setStart(endPonit);//起始点 
				line.setEnd(mapPoint);//终止点
				
				//画折线
				if(geoType == Geometry.Type.POLYLINE){
					Polyline polyline = new Polyline(); 
					polyline.addSegment(line, true);  
		            Graphic iGraphic=new Graphic(polyline,lineSymbol); 
		            graphicsLayer.addGraphic(iGraphic);
		            if(Variable.measureLength)
		            	measure(endPonit,mapPoint,geoType);
				}
				if(geoType == Geometry.Type.POLYGON){
					graphicsLayer.removeAll();
					  //画面
				      if(polygon==null){  
		                  polygon=new Polygon(); 
		                  webPolygon=new Polygon();
		              }  
				      Polygon polygon = new Polygon();  
				      Point startPoint = null;  
			          Point endPoint = null;  	
			          // 绘制完整的多边形  
			           for(int i=1;i<points.size();i++){  
			                startPoint = points.get(i-1);  
			                endPoint = points.get(i);  
			                Line line1 = new Line();  
			                line1.setStart(startPoint);  
			                line1.setEnd(endPoint);  
			                polygon.addSegment(line1, false);  
			              
			            }  		
			          Graphic gGraphic=new Graphic(polygon,fillSymbol);  
			          graphicsLayer.addGraphic(gGraphic);
			          if(Variable.measureArea)
			        	  measure(endPonit,mapPoint,geoType);
				}
			}
			//每一次的上一个点作为下一个的起点
            endPonit = mapPoint;			
		}
		return true;
	}
	
	/**
	 * 双击地图
	 */
	public boolean onDoubleTap(MotionEvent point) {
		if(geoType!=null){
			geoType = null;
			endPonit = null;
			//结束量测
			Variable.measureLength = false;
			Variable.measureArea =false;
			Toast.makeText(context, "结束绘制", Toast.LENGTH_SHORT).show();
		}
		return true;
	}
	
	/**
	 * 长按地图
	 */
	public void onLongPress(MotionEvent point) {
	}
    /**
     * 距离量测和面积量测
     * @param startPonit
     * @param endPonit
     * @param geoType
     */
	public void measure(Point startPonit,Point endPonit,Geometry.Type geoType){
		Point msPoint = (Point) GeometryEngine.project(startPonit ,map.getSpatialReference(),SpatialReference.create(102100));
		Point mePoint = (Point) GeometryEngine.project(endPonit ,map.getSpatialReference(),SpatialReference.create(102100));
		//两点连线
		Line line = new Line() ;
		line.setStart(mePoint);//起始点 
		line.setEnd(msPoint);//终止点
		
		if(geoType == Geometry.Type.POLYLINE ){
			//距离量测
			double mathLength = line.calculateLength2D();
			String  length = null;
			if(mathLength>1000){
				mathLength=mathLength/1000;
				String format = new DecimalFormat("0.00").format(mathLength);//保留小数点后两位
				length = format+"公里";
			}else{
				String format = new DecimalFormat("0.00").format(mathLength);//保留小数点后两位
				length = format+"米";
			}
			Toast.makeText(context, "距离:"+length, Toast.LENGTH_SHORT).show();
		}
		
		if(geoType == Geometry.Type.POLYGON ){ 
			Polygon polygon = new Polygon();  	  
            Point startPoint = null;  
            Point endPoint = null;  
            // 绘制完整的多边形  
            for(int i=1;i<points.size();i++)
            {  
            	startPoint = (Point) GeometryEngine.project(points.get(i-1) ,map.getSpatialReference(),SpatialReference.create(102100));
        		endPoint = (Point) GeometryEngine.project(points.get(i) ,map.getSpatialReference(),SpatialReference.create(102100));
                Line line1 = new Line();  
                line1.setStart(startPoint);  
                line1.setEnd(endPoint);  
                polygon.addSegment(line1, false);  
            }  			        
			double  mathArea2D = Math.abs(polygon.calculateArea2D());
			System.out.println(mathArea2D);
			String  Area = null;
			if(mathArea2D>1000000){
				mathArea2D=mathArea2D/1000000;
				String format = new DecimalFormat("0.00").format(mathArea2D);//保留小数点后两位
				Area = format+"平方公里";
			}else{
				String format = new DecimalFormat("0.00").format(mathArea2D);//保留小数点后两位
				Area = format+"平方米";
			}
			Toast.makeText(context, "面积:"+Area, Toast.LENGTH_SHORT).show();
		}
		
	}
 
}


原文 https://blog.csdn.net/gis_zzu/article/details/80023643