Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.