虽说标题是创建多线polyline,但是具体的代码自己可以变通,创建点、多点什么的都是ok的
两种方法,一种是逐个点选取,然后连接成一条多线;
另一种是直接画一条多线,具体区别后面说。
不管哪一种方法,前几步是一样的:
首先,添加MapView,在xml布局文件里添加
<com.esri.android.map.MapView
android:layout_width="match_parent"
android:layout_weight="4"
android:layout_height="0dp"
android:id="@+id/mapview">
</com.esri.android.map.MapView>
接着在java代码中新建MapView并使用findViewById的方法建立连接
MapView map;
map = (MapView) findViewById(R.id.sharemapview);
以上两步是最基础的。
然后想想,既然要“画”出polyline,那么map里必须加一个GraphicsLayer图层,当然最基础的图层也是要有的,不然这条失去空间意义的polyline就没什么意义了。
具体步骤是:1、添加底图;2、添加GraphicsLayer图层;最好是按这个顺序添加
GraphicsLayer graphicsLayer;
graphicsLayer = new GraphicsLayer();
String strMapUrl = "http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer";
map.addLayer(new ArcGISTiledMapServiceLayer(strMapUrl));
map.addLayer(graphicsLayer);
以上代码可能不是完全连在一起的,具体哪一句该放哪里,自己应该清楚。
接下来写一个内部类继承MapOnTouchListener,用于处理touch事件
根据这个内部类的不同写法区分这两种方法:
第一种方法:逐点构建polyline
public class DrawGraphicTouchListener extends MapOnTouchListener {
Point startPoint = null;
public DrawGraphicTouchListener(Context context, MapView view) {
super(context, view);
}
@Override
public boolean onSingleTap(MotionEvent point) {
/*
* 下面注释的代码是以逐点形式绘制Polyline的
*/
float x=point.getX();
float y=point.getY();
Point ptCurrent=map.toMapPoint(x,y);
pointsList.add(ptCurrent);
if (ptStart==null) {
ptStart=ptCurrent;
Graphic pgraphic=new Graphic(ptStart,sbl);
graphicsLayer.addGraphic(pgraphic);
}else{
Graphic pGraphic=new Graphic(ptCurrent,new
SimpleMarkerSymbol(Color.RED, 8,
SimpleMarkerSymbol.STYLE.CIRCLE));
graphicsLayer.addGraphic(pGraphic);
Line line=new Line();
line.setStart(ptPrevious);
line.setEnd(ptCurrent);
if(drawType==Geometry.Type.POLYLINE){
Polyline polyline=new Polyline();
polyline.addSegment(line, true);
Graphic iGraphic=new Graphic(polyline, sbl);
graphicsLayer.addGraphic(iGraphic);
}
}
return false;
}
public boolean onDragPointerMove(MotionEvent from, MotionEvent to) {
return true;
}
@Override
public boolean onDragPointerUp(MotionEvent from, MotionEvent to) {
return true;
}
}
第二种方法:直接画polyline
public class DrawGraphicTouchListener extends MapOnTouchListener {
Point startPoint = null;
public DrawGraphicTouchListener(Context context, MapView view) {
super(context, view);
}
@Override
public boolean onSingleTap(MotionEvent point) {
return false;
}
public boolean onDragPointerMove(MotionEvent from, MotionEvent to) {
if(isChoose==true){
Point mapPt = map.toMapPoint(to.getX(), to.getY());
/*
* if StartPoint is null, create a polyline and start a path.
*/
if (startPoint == null) {
graphicsLayer.removeAll();
poly = new Polyline();
startPoint = map.toMapPoint(from.getX(), from.getY());
poly.startPath((float) startPoint.getX(),
(float) startPoint.getY());
/*
* Create a Graphic and add polyline geometry
*/
Graphic graphic = new Graphic(startPoint, new SimpleLineSymbol(
Color.RED, 5));
/*
* add the updated graphic to graphics layer
*/
graphicsLayer.addGraphic(graphic);
}
poly.lineTo((float) mapPt.getX(), (float) mapPt.getY());
}
return true;
}
@Override
public boolean onDragPointerUp(MotionEvent from, MotionEvent to) {
/*
* When user releases finger, add the last point to polyline.
*/
if(isChoose==true){
graphicsLayer.addGraphic(new Graphic(poly, new SimpleLineSymbol(
Color.RED, 5)));
startPoint = null;
}
return true;
}
}
第一种方法的效果是一个一个点连接起来形成一条polyline,显得很不平滑,但是使用一般数据库存储也没太大问题,
第二种方法用一般数据库存储会比较困难一些,但是体验良好,画出来的polyline很平滑,比较好看。
下面是第一种方法的效果图:(第一张);其他三张就是第二种方法的效果了,明显优美多了~
就酱