-1

I am successfully making and drawing poly lines on a marker click event. But there is a thing happening which I do not want to be happen.

And that is when I click on marker Let say JackiMarker it draws route to the target location but then I click to second marker let say MichaelMarker it also draws the route but the previous marker is still there which I do not want to be.

I am sing this code to draw and remove the marker. It has the clearRoute function but it is not working for me , please help me what to do . Please help me in removing the last polyline I have drawn on map.

Coas Mckey
  • 701
  • 1
  • 13
  • 39

2 Answers2

1

Please try this,

Polyline polyline = this.mMap.addPolyline(new PolylineOptions().....);

Then when you want to remove it:

polyline.remove();

If you have lots of Polylines, just add them to a List as they are put on the map:

List<Polyline> polylines = new ArrayList<Polyline>();

for(....)
{
  polylines.add(this.mMap.addPolyline(new PolylineOptions()....));

}

And when you want to delete:

for(Polyline line : polylines)
{
line.remove();
}
polylines.clear();

The key is to keep a reference to the Polyline objects and call .remove() on each one.

M D
  • 47,665
  • 9
  • 93
  • 114
0

It is 100 % working code which I shared in the question , the problem was in my end . what I was doing , was trying to delete the path when clicked on the marker and on the OnMarkerClickListener I was initiating the Route Class , which created another instance of the same class thus creating the array of polylines from 0 index which Is why i was not getting into the for loop of the clearRoute method.

so by instantiating the Route class as a global solved my problem.

Coas Mckey
  • 701
  • 1
  • 13
  • 39