我認爲最簡單的方法是將覆蓋類繼承,然後重寫draw方法。繪製方法非常開放,繪製路徑不應太難。一個例子是這個樣子:然後
public class PathOverlay extends Overlay{
private List<GeoPoint> gpoints;
public PathOverlay(List<GeoPoint> gpoints){
this.gpoints = gpoints;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
{
List<Point> mpoints = new ArrayList<Point>();
// Convert to a point that can be drawn on the map.
for(GeoPoint g : gpoints){
Point tpoint = new Point();
mapView.getProjection().toPixels(g, tpoint);
mpoints.add(tpoint);
}
Path path = new Path();
// Create a path from the points
path.moveTo(mpoints.get(0).x, mpoints.get(0).y);
for(Point p : mpoints){
path.lineTo(p.x, p.y);
}
Paint paint = new Paint();
paint.setARGB(255, 255, 0, 0);
paint.setStyle(Style.STROKE);
// Draw to the map
canvas.drawPath(path,paint);
return true;
}
}
這個類的對象可以被添加到通過調用MapView.getOverlays返回的列表()添加到地圖中。