一:实现绘图功能的思路
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 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();
}
}
}