Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all the polylines from a map

Following

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))
                .width(2).color(Color.RED).geodesic(true));
    }

One solution i can get is

map.clear();

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

like image 806
Archie.bpgc Avatar asked Feb 13 '13 12:02

Archie.bpgc


People also ask

How do I remove a polyline from Google Maps flutter?

you need to clear polylineCoordinates before plotting new paths. polylineCoordinates. clear(); // call this before checking result value.

How do you remove map marks?

2. Click the "My Places" button just beneath the search bar. Click "Maps" and wait for the list of maps to appear down the left side of the screen. When they do, click the title of the map containing the marker that you want to remove.


2 Answers

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:

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.

like image 110
DiscDev Avatar answered Oct 22 '22 08:10

DiscDev


I know this is very old question but I noticed that this is very common need. I found another way and I wanted to share it.

Here is the basic idea:

Polyline polylineFinal;
PolylineOptions polylineOptions;

loop {

    polylineOptions.add( new LatLng( latitude, longitude ) );

}

polylineOptions.width(2);
polylineOptions.color(Color.RED);
polylineOptions.geodesic(true);

polylineFinal = map.addPolyline (polylineOptions);

Map's "addPolyline" method returns a single polyline which contains all the points. When I need to remove the points, I call polylineFinal's "remove" method.

polylineFinal.remove();
like image 7
kizanlik Avatar answered Oct 22 '22 07:10

kizanlik