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!
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With