arcgis for android 实现绘图功能
程序员文章站
2022-05-25 15:42:25
一:实现绘图功能的思路
1:首先需要一个点击地图的一个监听函数,可以实现点击地图画点、线、面。arcgis提供一个mapontouchlistener类。
2:创建点、线、面对象,arcgis提供的...
一:实现绘图功能的思路
1:首先需要一个点击地图的一个监听函数,可以实现点击地图画点、线、面。arcgis提供一个mapontouchlistener类。
2:创建点、线、面对象,arcgis提供的类有point,polygon,line
3:设置样式,不管是点、线、面都有自己的样式。arcgis提供了simplelinesymbol (线样式类)simplemarkersymbol(点样式类)simplefillsymbol(面样式类)
4:创建一个绘画的图层,graphicslayer
5:将绘画图层添加到地图上。
二:实现代码
1:创建一个类继承mapontouchlistener用来监听地图的点击
public class maptouchlistener extends mapontouchlistener { /** * 单击地图 */ public boolean onsingletap(motionevent point) { } }
2:创建点、线、面对象
//屏幕坐标转化成空间坐标 point mappoint = map.tomappoint(point.getx(),point.gety());3:创建样式
//设置点、线、面的样式 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);//设置的透明度 }4:创建一个绘画图层
//创建绘图图层 对象 drawlayer = new graphicslayer(); mmapview.addlayer(drawlayer);
5:添加到地图上(这一定要加上,否则地图的监听事件无法实现)
//绑定触摸事件监听器 maptouchlistener=new maptouchlistener(earthquakeactivity.this,mmapview); maptouchlistener.setlayer(drawlayer); 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 arraylistpoints=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 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 ();i++){>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(); } } } ();i++)>