Ad

How To REMOVE A Marker By ID In Leaflet.MarkerClusterGroup

I have no idea how to remove a marker with particular id in a clusterGroup layer. I have tried to remove the marker with map.removeLayer(marker[id]) and which is not working in a clustergroup. I have done a simple Jsfiddle app here which shown the problem of removing particular marker with id. The id assigned for each marker is using object method.

Wish to get help from anyone and any help is appreciate. Thank you.

app ->https://jsfiddle.net/wesleylim97/y76gbjq2/30/

Ad

Answer

Remove the markers from the cluster group, instead of removing them from the map.

So if you're doing...

clustergroup.addLayer(marker)

...or...

marker.addTo(clustergroup)

...to add the marker, then the opposite action is...

clustergroup.removeLayer(marker)

...or...

marker.removeFrom(clustergroup)

Note that removeLayer is explicitly mentioned in the Leaflet.MarkerCluster documentation.

Ad
source: stackoverflow.com
Ad