upendra
upendra

Reputation: 349

Delete all polyline from map before creating new polyline

I am using leaflet to show the map and create the polyline. I want to delete all the polyline before creating any new polyline.

When I am creating polyline ,

I stored all polyline in array - $scope.polycoords = [] ;

To remove all polyline I am using this code.

for(var i = 0; i< $scope.polycoords.length; i++){
   map.removeLayer($scope.polycoords[i]);
}

But its not working. Giving me this error - Error: map.removeLayer is not a function
How can I solve this ?

Upvotes: 1

Views: 2660

Answers (1)

Patrick D
Patrick D

Reputation: 91

May it's to late for you but this works for me:

Use L.featureGroup():

var active_polyline = L.featureGroup().addTo(map);

When you create the first polylines, add each of them to the active_polyline thru a loop

polyline = L.polyline(mypolyline).addTo(active_polyline);

Empty the L.featureGroup() before creating a new polyline in order to delete the previous ones:

active_polyline.clearLayers();

Then create the new one:

polyline = L.polyline(mynewpolyline).addTo(active_polyline);

Upvotes: 2

Related Questions