项目中需要实现能够在地图上通过手绘的方式绘制线图形,已达到辅助显示效果,实现并不复杂,这里面主要用到了一个坐标转换:屏幕坐标转换到地图坐标,记录手指触摸屏幕的点并保存到数组中,并绘制到地图上,具体代码实现如下:
//注册触摸事件
aMap.setOnMapTouchListener(new OnMapTouchListener() {
@Override
public void onTouch(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
System.out.println("onTouch map ACTION_UP");
break;
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
if (isdrawing) {
Point touchpt = new Point();
touchpt.x = (int) event.getX();
touchpt.y = (int) event.getY();
//重点看这里,坐标转换
LatLng latlng = mapView.getMap().getProjection()
.fromScreenLocation(touchpt);
listPts.add(latlng);
// 地图上绘制
drawLine(listPts);
}
break;
default:
break;
}
}
});
绘制线的方法:
/**
* 地图上绘制线
* @param pts
*/
private void drawLine(List<LatLng> pts) {
if (pts.size() >= 2) {
if (pts.size() == 2) {
polyline = aMap.addPolyline((new PolylineOptions()).addAll(pts)
.width(10).setDottedLine(true).geodesic(true)
.color(Color.argb(255, 1, 1, 1)));
} else {
polyline.setPoints(pts);
}
}
}