Using leaflet.js, how do I iterate through markers in a cluster?

leaflet, markerclusterer

Solution

You can't iterate though markers in a map or a cluster.

Create an array where you push the markers when you create them.

Iterate through your array

When you have to remove a marker, use

if(cluster.hasLayer(marker) cluster.removeLayer(marker);
if(map.hasLayer(marker) map.removeLayer(marker);
// remove marker from array (easier with a jQuery Array)

Problem

Se we have a map and at a certain zoom level we start clustering the markers. Now I want to be able to delete certain markers. I can delete the markers that don't participate in a cluster but the markers in a cluster do not get deleted because the code doesn't iterate through them. I'd post code but it's all over the place and quite specific. I can do the following; ``` $.each(MAP._layers, function (i, layer) { if (layer.feature) { var marker = LIGHTWEIGHT_BUILDING_MAPPING[layer.feature.id]; MAP.removeLayer(marker); } }); ``` And all the visible markers are removed but not the ones within a cluster. Any thoughts?

Original source