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

前端 未结 1 939
生来不讨喜
生来不讨喜 2021-01-01 06:45

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 metho

相关标签:
1条回答
  • 2021-01-01 07:35

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题