How to draw a dynamic line (route) with Google Maps Android API v2

懵懂的女人 提交于 2020-01-22 12:47:05

问题


I'm wondering what the best practice is to draw a dynamic route on a map with the Google Maps API v2. I want to have a map that's able to prolong the route while the user is moving. There seems to be the obvious solution by using a Polyline and PolylineOptions. But I just can't find an easy way to add points after I instantiated the Polyline. To draw a Polyline is something like this:

PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.add(POINT1, POINT2, POINT3);
Polyline line = googleMap.addPolyline(polylineOptions);

But after I passed the line to GoogleMap I can't add any new points to it. Something like

polylineOptions.add(POINT1, POINT2, POINT3);

doesn't add anything to my route.

I could just add complete new Polyline. But isn't there a way to prolong just the existing one? I figured out a way by getting all the points of the Polyline, add the new point, and write them back to the line:

List<LatLng> points = line.getPoints();
points.add(POINT4);
line.setPoints(points);

But it seems to be cumbersome to me. Any ideas?


回答1:


In the mainActivity class, define a private static LatLng variable named prev and initialize it to (0,0) first. Also make a flag variable and assign 0 to it. In the Listener's OnLocationChanged method, create a local variable LatLng named current and get current co-ordinates here... check the value of flag first, if it is 0 then assign current to prev. Then add a polyline.

Assign current to prev again (this will happen every time as after the first time, the flag will be 1)

For example:

public void onLocationChanged(Location location) 
{

    LatLng current = new LatLng(location.getLatitude(), location.getLongitude());

    if(flag==0)  //when the first update comes, we have no previous points,hence this 
    {
        prev=current;
        flag=1;
    }
    CameraUpdate update = CameraUpdateFactory.newLatLngZoom(current, 16);
    map.animateCamera(update);
    map.addPolyline((new PolylineOptions())
        .add(prev, current).width(6).color(Color.BLUE)
        .visible(true));
    prev=current;
    current = null;                    
}

Something like this. Of course performance improvements can be made, this is just an example code. But it should work. Every time the polyline will add only the previous and current point, thereby extending it point by point.




回答2:


Looking at the documentation, it appears that polylineOptions.add(LatLng) and googleMap.addPolyline(polylineOptions) methods return the polylineOptions object. The first method will also return polylineOptions WITH the point added to the end.

I think you'll have to add the polylineOptions to googleMap.addPolyline(polylineOptions) again or use googleMap.clear() before adding it a second time. Somehting like this:

polylineOptions = googleMap.addPolyline(polylineOptions);
// googleMap.clear();
googleMap.addPolyline(polylineOptions.add(point));


来源:https://stackoverflow.com/questions/13933411/how-to-draw-a-dynamic-line-route-with-google-maps-android-api-v2

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