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

Maw*_*awg 3 leaflet leaflet.markercluster

我正在使用Leaflet.markercluster

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

    // 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()
Run Code Online (Sandbox Code Playgroud)

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


相关问题:

ghy*_*ybs 6

确实,Leaflet.markercluster插件的使用方法可能会有些混乱:

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

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

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

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

为了完整起见,MCG 还具有 Leaflet.markercluster 文档中称为“组方法”的方法

方法getChildCount()getAllChildMarkers())用于簇标记,例如,您在使用"clusterclick"事件等时得到的。