在 Android 中步行/驾驶时更新位置标记并在 Google 地图上绘制路径 [英] Update Location Marker and draw path on Google Maps when walking/driving in Android

查看:11
本文介绍了在 Android 中步行/驾驶时更新位置标记并在 Google 地图上绘制路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个类似于 Google 地图的功能.每当有人开始步行/开车时,我都想更新我的位置标记.我正在考虑使用 LocationListner 的 onLocationChange 方法,但我不确定.我也喜欢在谷歌地图上绘制更新位置的路径.

I want to create a functionality like Google Maps. I want to update my location marker whenever a person start walking/driving. I am thinking of using onLocationChange method of LocationListner but I am not sure. I also like to draw the path with updating location on the Google Maps.

欢迎所有建议.

ItemizedOverlay:-

ItemizedOverlay:-

public class LocationOverlay extends ItemizedOverlay<OverlayItem>{

    private List<OverlayItem> myOverlays;
    Path path;
    static int cnt = -1;
    public LocationOverlay(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
        System.out.println("Normal...");
        myOverlays = new ArrayList<OverlayItem>();
    }


    public void addOverlay(OverlayItem overlay) {
        System.out.println("Add Overlay called");
        myOverlays.add(overlay);
        populate();
            cnt++;
    }

    @Override
    protected OverlayItem createItem(int i) {
        return myOverlays.get(i);
    }

    public void removeItem(int i) {
        myOverlays.remove(i);
        populate();
    }

    @Override
    public int size() {
        return myOverlays.size();
    }

    @Override
    public void draw(Canvas canvas, final MapView mapView, boolean shadow) {


           if (shadow) {
            paint.setDither(true);
            paint.setColor(Color.RED);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStrokeCap(Paint.Cap.ROUND);
            paint.setStrokeWidth(4);

            path = new Path();
            Point point1 = new Point();
            Projection projection = mapView.getProjection();
            if (myOverlays.size() > 1) {

                System.out.println(">>>>>>>>>>>");

                for (int i = 0, j=i; i < myOverlays.size(); i++) {
                Point point1 = new Point();
                Point point2 = new Point();
                projection.toPixels(myOverlays.get(i).getPoint(), point1);

                System.out.println(">>>>>>>>>>>");

                projection.toPixels(myOverlays.get(j++).getPoint(), point2);
                System.out.println("Point == " + j);

                path.moveTo(point2.x, point2.y);
                path.lineTo(point1.x, point1.y);
                canvas.drawPath(path, paint);
                super.draw(canvas, mapView, shadow);
            }   
            }
        }
        canvas.drawPath(path, paint);
        super.draw(canvas, mapView, shadow);
    }
}

注意 - 对于单个 locationChanged,draw 方法会被多次调用.

-

每当发生任何位置更改事件触发时,我都可以绘制路线,但我在那里遇到了一个小问题.它也每次都从根目录绘制路径.我希望它从最后一个位置开始绘制路径(即从最后一个位置连接到下一个位置).我附上一张图片以便更好地理解.

I am able to draw the route whenever there is any location changed event fire but I am facing a small problem there. Its also drawing path from root every time. I want that from last place it should start drawing the path (i.e connecting from last to next location). I am attaching one image for better understanding.

从图中可以看出,起点也与终点相连.我不想要这个.对此有任何想法.

From the image you can see that the starting point is also connected to last point. I don't want this. Any idea on this.

推荐答案

使用 MyLocationOverlay.您需要做的就是将此叠加层添加到您的 MapView.比如:

Current location can be easily drawn with MyLocationOverlay. What you need to do - is to add this overlay to your MapView. Something like:

mapView.addOverlay(new MyLocationOverlay(context, mapView));

默认位置点会自动绘制,一移动就会更新

The default location dot will be drawn automatically and will be updated as soon as you move

路线有点棘手,但仍然没有火箭科学.您需要做的是扩展 Overlay 类并实现 draw() 逻辑.您需要跟踪您旅行的 GeoPoints 并将它们投影到地图上(在您的叠加层的 draw() 中,您可以向 mapView 询问 Projection 实例 - getProjection().您可以使用此投影将 GeoPoints 映射到您的地图坐标).一旦您拥有 GeoPoints 列表 - 您可以创建一个 Path 和在画布上绘制此路径

With route it is a bit trickier, but still there is no rocket science in there. What you need to do - is to extend Overlay class and implement draw() logic. You need to keep track of GeoPoints you travelled and project them on a map (in your overlay's draw() you can ask your mapView for a Projection instance - getProjection(). You can use this projection to map GeoPoints to your map coordinates). As soon as you have list of GeoPoints - you can create a Path and draw this Path on a Canvas

如何在地图上绘制路径的简化示例(我的项目中使用了一些自定义类,但你应该明白):

The simplified example how to draw path on a map (there are some custom classes I use in my project, but you should get an idea):

Path path;

@Override
public void draw(Canvas canvas, final MapView mapView, boolean shadow)
{
    Projection proj = mapView.getProjection();
    updatePath(mapView,proj);
    canvas.drawPath(path, mPaint);
}

protected void updatePath(final MapView mapView, Projection proj)
{
    path = new Path();

    proj.toPixels(mPoints.get(0).point, p);
    path.moveTo(p.x, p.y);

    for(RouteEntity.RoutePoint rp : mPoints)
    {
        proj.toPixels(rp.point, mPoint);
        path.lineTo(mPoint.x, mPoint.y);
    }
}

这篇关于在 Android 中步行/驾驶时更新位置标记并在 Google 地图上绘制路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆