Reputation: 1635
I monitor current location and register GoogleMap.MyLocationChangeListener
. I want to draw a polyline (track on the map) that represents my own route. On each location update, I'd like to add new point to the route, so that the track on the map is updated.
Here is my code that doesn't work:
private GoogleMap mMap;
private boolean drawTrack = true;
private Polyline route = null;
private PolylineOptions routeOpts = null;
private void startTracking() {
if (mMap != null) {
routeOpts = new PolylineOptions()
.color(Color.BLUE)
.width(2 /* TODO: respect density! */)
.geodesic(true);
route = mMap.addPolyline(routeOpts);
route.setVisible(drawTrack);
mMap.setOnMyLocationChangeListener(this);
}
}
private void stopTracking() {
if (mMap != null)
mMap.setOnMyLocationChangeListener(null);
if (route != null)
route.remove();
route = null;
}
routeOpts = null;
}
public void onMyLocationChange(Location location) {
if (routeOpts != null) {
LatLng myLatLng = new LatLng(location.getLatitude(), location.getLongitude());
routeOpts.add(myLatLng);
}
}
How to add points to the polyline, so that the changes will be reflected in the UI? Right now the polyline is not rendered.
I am using the latest play-services:6.1.71
(as of this date).
Upvotes: 0
Views: 3434
Reputation: 1635
This appears to work for me:
public void onMyLocationChange(Location location) {
if (routeOpts != null) {
LatLng myLatLng = new LatLng(location.getLatitude(), location.getLongitude());
List<LatLng> points = route.getPoints();
points.add(myLatLng);
route.setPoints(points);
}
}
Is there a better way?
Upvotes: 1