欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

arcgis for android实现量测详细教程

程序员文章站 2022-06-01 20:33:48
一:思路 注意:量测功能是在web墨卡托坐标系下实现,如果使用天地图(wgs-84坐标)做底图,不进行坐标转化会出现量测错误。 1:首先进行画线,画面。 2:量测 在距离量测的arcgis提供了li...

一:思路

注意:量测功能是在web墨卡托坐标系下实现,如果使用天地图(wgs-84坐标)做底图,不进行坐标转化会出现量测错误。

1:首先进行画线,画面。

2:量测

在距离量测的arcgis提供了line.calculatelength2d();来实现

在面积量测arcgis提供了polygon.calculatearea2d()来实现。

二:实现的具体方法

 /**
     * 距离量测和面积量测
     * @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;i1000000){
				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();
		}
		();i++)>

三:全部代码

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;i1000){
				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;i1000000){
				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();
		}
		
	}

}
();i++)>();i++){>
 还需要在mapview中实现
  //创建绘图图层 对象
	 drawlayer = new graphicslayer();
	 mmapview.addlayer(drawlayer);
	 //绑定触摸事件监听器
	 maptouchlistener=new maptouchlistener(earthquakeactivity.this,mmapview);
	 maptouchlistener.setlayer(drawlayer);
   mmapview.setontouchlistener(maptouchlistener);