Ask Your Question
3

What is the procedure to remove all markers and layers from a Leaflet map in order to add new ones?

asked 2022-04-12 11:00:00 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-02-12 21:00:00 +0000

djk gravatar image

To remove all markers and layers from a Leaflet map, you can use the following steps:

  1. Retrieve the map object using the leaflet() function.
  2. Use the clearLayers() method on the map object to remove all existing layers and markers.
  3. Create new layers and markers, and add them to the map object using the addLayer() method.

Example code:

var map = L.map('map');

map.clearLayers(); //Remove all existing layers and markers

// Create new layers and markers
var marker = L.marker([51.5, -0.09]).addTo(map);
var polyline = L.polyline([[51.5, -0.09], [51.3, -0.1]]).addTo(map);

Note: If you have multiple layers and markers that you want to remove, you can use the getLayers() method on the map object to retrieve all existing layers, and then remove them one by one using the removeLayer() method.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-04-12 11:00:00 +0000

Seen: 11 times

Last updated: Feb 12 '22