Skip to content

Instantly share code, notes, and snippets.

@tomasevich
Created March 3, 2020 08:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomasevich/5bf989ab2d1578382ab06f58badd5bc3 to your computer and use it in GitHub Desktop.
Save tomasevich/5bf989ab2d1578382ab06f58badd5bc3 to your computer and use it in GitHub Desktop.
Remove/delete all markers from map (leaflet)

I use markercluster

var markers = L.markerClusterGroup()

/* delete method */
map.eachLayer(function(layer) {
    if (layer instanceof L.MarkerClusterGroup)
    {
        map.removeLayer(layer)
    }
})

dataset.forEach(function(item)
{
    var marker = L.marker(item.coordinates, {
        icon: L.divIcon({
            html: `<div><span>1</span></div>`,
            className: 'leaflet-div-icon'
        })
    })

    markers.addLayer(marker)
})

map.addLayer(markers)
@afarber
Copy link

afarber commented Apr 1, 2023

There is an easier way: markers.clearLayers()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment