Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 715
griegs Avatar asked Jul 16 '14 04:07

griegs


1 Answers

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)
like image 116
YaFred Avatar answered Nov 15 '22 21:11

YaFred