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);
}
}
}
Android中ArcGIS中实现DrawTool类
最新推荐文章于 2021-05-26 13:05:57 发布