I am trying to remove previously added Polyline and redraw new Polyline when the location has been changed. I tried both
this.routeToDestination.setPoints(pointsToDestination) and this.routeToDestination.remove()
but neither of them worked.
I followed How to draw a dynamic line (route) with Google Maps Android API v2 but could not resolved the issue
@Override
public void onResume() {
super.onResume();
routeToDestination = mMap.addPolyline(new PolylineOptions()
.add(new LatLng(location.getLatitude(), location.getLongitude()),
new LatLng(this.destinationLatitude, this.destinationLongitude))
.width(1)
.color(Color.DKGRAY)
);
}
@Override
public void onLocationChanged(Location location) {
List<LatLng> pointsToDestination = new ArrayList<LatLng>();
pointsToDestination.add(new LatLng(location.getLatitude(), location.getLongitude()));
pointsToDestination.add(new LatLng(destinationLatitude, destinationLongitude));
this.routeToDestination.setPoints(pointsToDestination);
}
}
To remove a polyline you should simply use remove() method as stated in the API.
//Add line to map
Polyline line = mMap.addPolyline(new PolylineOptions()
.add(new LatLng(location.getLatitude(), location.getLongitude()),
new LatLng(this.destinationLatitude, this.destinationLongitude))
.width(1)
.color(Color.DKGRAY)
//Remove the same line from map
line.remove();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With