我正在开发一个应用程序,我需要在 Google map 上绘制一条路径,因为位置会发生变化,以显示用户去过的地方。我曾尝试使用 MyLocationOverlay 类,认为我可以重写绘制位置的方法,但我一直未能确定要重写的方法。此外,似乎每次绘制位置时 MyLocationOverlay 都会绘制一张新 map 。我目前正在使用 ItemizedOverlay 并且每次位置更改时都会在列表中添加一个点。这行得通,我走路时得到一条虚线路径,但我真的想要一条实心路径。有什么建议吗?
我也看过这个post但我无法让它工作。不需要叠加层才能将其显示在 map 上吗?
最佳答案
我认为最简单的方法是继承 Overlay 类,然后覆盖 draw 方法。 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() 返回的列表中以添加到 map 中。
关于java - 根据您在 Android 版 Google map 上的位置绘制一条路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5914283/