Update Location Marker and draw path on Google Maps when walking/driving in Android

不问归期 提交于 2019-12-30 03:36:04

问题


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.

All suggestions are most welcome.

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);
    }
}

NOTE - draw method gets called multiple time for single locationChanged.

EDIT :-

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.


回答1:


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

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);
    }
}


来源:https://stackoverflow.com/questions/11745913/update-location-marker-and-draw-path-on-google-maps-when-walking-driving-in-andr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!