jirinovo
jirinovo

Reputation: 2275

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!

Upvotes: 7

Views: 8822

Answers (1)

Iavor
Iavor

Reputation: 2077

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

Upvotes: 12

Related Questions