Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leaflet MarkerCluster - how to get markers inside cluster on event (mouseover, mouseclick)?

I need to get the markers inside a cluster when the specific event is fired (clustermouseover or clusterclick). I was examining the event object e in developer console, but e.layer._markers is an empty array.

group_markers.on('clustermouseover', function (e) {
    console.log('clustermouseover');
    console.log(e);
});

group_markers is the L.markerClusterGroup object.

Thanks in advance for help!

like image 985
jirinovo Avatar asked Mar 13 '18 17:03

jirinovo


1 Answers

Use the getAllChildMarkers method of the layer object (e.layer). Example:

markers.on('clustermouseover', function (e) {
    console.log('Number of markers: ' + e.layer.getAllChildMarkers().length);
});

According to the Leaflet.markercluster docs

getAllChildMarkers: Returns the array of total markers contained within that cluster.

Here's a JSBin with a working example.

An underscore (_) is usually used to denote that an object property (such as _markers) or method is private. Generally, you don't want to access private object members as they are only supposed to be used internally.

Side note:

If you search through the event's properties in your console, look through the prototype of the layer object and you'll find the getAllChildMarkers method:

Layer prototype properties

like image 102
Iavor Avatar answered Nov 14 '22 23:11

Iavor