我需要在触发特定事件(clustermouseover
或 clusterclick
)时获取集群内的标记。我正在开发人员控制台中检查事件对象 e
,但 e.layer._markers
是一个空数组。
group_markers.on('clustermouseover', function (e) {
console.log('clustermouseover');
console.log(e);
});
group_markers
是 L.markerClusterGroup
对象。
在此先感谢您的帮助!
最佳答案
使用图层对象(e.layer
)的getAllChildMarkers
方法。示例:
markers.on('clustermouseover', function (e) {
console.log('Number of markers: ' + e.layer.getAllChildMarkers().length);
});
getAllChildMarkers: Returns the array of total markers contained within that cluster.
这是 a JSBin有一个工作示例。
下划线(_
)通常用于表示对象属性(例如_markers
)或方法是私有(private)的。通常,您不想访问私有(private)对象成员,因为它们只应在内部使用。
旁注:
如果您在控制台中搜索事件的属性,查看 layer
对象的原型(prototype),您将找到 getAllChildMarkers
方法:
关于javascript - Leaflet MarkerCluster - 如何在事件(鼠标悬停,鼠标单击)时获取集群内的标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49262404/