java - 根据您在 Android 版 Google map 上的位置绘制一条路径

标签 java android google-maps

我正在开发一个应用程序,我需要在 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/

相关文章:

java - 第一次尝试尝试共同的偏好

java - Maven - 外部库中的依赖项不会复制到目标文件夹

android - 围绕其中心旋转 ImageView 而不切断其边缘

android - 如何限制 react native packager (android)(gradle) 中的 worker 数量?

Android:从 fragment 中的 ArrayList 添加多边形到 map

java - 如何让 Maven 检查源行尾?

javascript - 将全局函数/变量添加到 GraalVM JavaScript 引擎

google-maps - 如何访问谷歌地图中某个地点的事件数据?

android osmdroid 将本地语言更改为英语

javascript - 如何检查手机是否至少使用过一次Google map