After trying all the previous answers on StackOverflow
I am posting this answer.
I am showing live location of a driver and a path from that location to a specific destination. What i want is to remove the old polyline soon the live location of driver changes but it's not hapening and it's increasing number of polylines.
Here is the code where i am removing the polyline but it's not working.
private void drawRouteOnMap(GoogleMap map, List<LatLng> positions) {
PolylineOptions options = new PolylineOptions().width(12).color(Color.GREEN).geodesic(true);
options.addAll(positions);
if (polyline != null) {
polyline.remove();
Log.e("Old Poly", "Removed");
}
Log.e("Poly", "Added");
polyline = mMap.addPolyline(options);
}
I have solved it myself. It is because that function
was being called from another call
due to which polyline
instance was null. Solved it by saving polyline
in SharedPrefrence
and retrieving that value again on next call.