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 m
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.