Archie.bpgc Archie.bpgc - 1 year ago 168
Android Question

How to remove all the polylines from a map


How to draw a path between two markers

I had to add lot of polylines between two markers, to make a path.

One of the markers is draggable, lets say source is draggable.

So, when user starts dragging the marker, the path previously drawn must be erased and a new path between new source and destination must be draw.

I am able to draw the new path, but how can i erase the previous path?

This is how the path is drawn:

for (int z = 0; z < list.size() - 1; z++) {
LatLng src = list.get(z);
LatLng dest = list.get(z + 1);
Polyline line = map.addPolyline(new PolylineOptions()
.add(new LatLng(src.latitude, src.longitude),
new LatLng(dest.latitude, dest.longitude))

One solution i can get is


To clear all the polylines, markers etc.. and add the markers again, then drawn the path.

But as soon as I start dragging, the marker is cleared, hence not visible on the map :(

Thank You

Answer Source

Keep track of the Polyline as you add it to the map:

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

Then when you want to remove it:


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>();

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

And when you want to delete:

for(Polyline line : polylines)


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download