Leaflet.markercluster - 如何计算markerClusterGroup 中的所有标记?

问题描述

我正在使用 @L_404_0@。

这是我的代码,基于各种相关问题:

    // Takes an L.markerClusterGroup as input parameter
    Self.GetNumMarkersInClusterGroup = function(clusterGroup)
    {
        Self.map.eachLayer(function (layer) {
            if (layer.getChildCount) {
// somehow need to check here for our desired cluter group

                console.log('Cluster group has ' + layer._childClusters.length + 'layers')        ;
                console.log('With a total of ' + layer._childCount + ' markers');  
                }
        });
    }       // GetNumMarkersInClusterGroup()

但是,layer.getChildCount 是未定义的 :-( 我做错了什么?


相关问题:

解决方法

确实在使用 Leaflet.markercluster 插件时可能会有些混乱:

  • 有整个 MarkerClusterGroup (MCG) 对象,即调用 L.markerClusterGroup() 时得到的内容
  • 集群标记,即当您的各个标记聚集在一起时 MCG 在地图上显示的内容
  • 您添加到 MCG 中的各个标记

如果您的 clusterGroup 实际上是一个 MCG,那么您可以简单地使用它扩展 Leaflet 标准特征组和层组这一事实,特别是它有一个 getLayers() method

返回添加到组中的所有图层的数组。

因此,如果您想要 MCG 中标记的总数,您可以执行例如clusterGroup.getLayers().length

为了完整起见,MCG 还具有在 Leaflet.markercluster 文档中称为 "group methods"

的方法

getChildCount() method(和 getAllChildMarkers())用于集群标记,例如您在使用 "clusterclick" 事件等时会得到什么。