The project needs to realize the ability to draw line graphics on the map by hand-drawing, which has achieved the effect of auxiliary display, and the implementation is not complicated. Here, a coordinate conversion is mainly used: screen coordinates are converted to map coordinates, and the point at which the finger touches the screen is recorded. Save it to the array and draw it on the map. The specific code is implemented as follows:
//注册触摸事件
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;
}
}
});
How to draw a line:
/**
* 地图上绘制线
* @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);
}
}
}