我正在制作一张传单地图,其中有许多标记与prunecluster一起显示,我需要获取地图上当前显示的聚类列表。
我已经用
初始化了集群var pruneCluster = new PruneClusterForLeaflet(150, 70);
我在开发人员工具和代码中看到的集群的唯一数组是pruneCluster.Cluster._clusters
。实际上,它几乎是地图中显示的簇的精确表示,但有时,该数组的长度与地图中显示的簇的数量不同。也就是说,该数组不代表我在地图上的情况。
我应该使用什么数组?
答案 0 :(得分:0)
pruneCluster.Cluster._clusters
包含它计算出的所有聚类,但是由于您可以平移和缩放地图,因此可见聚类可以是其子集。
您可以遍历集群并测试它们是否可见。像这样:
// Get the bounds of the map visible in the browser
// 'map' is your Leaflet map
var boundsOfView = map.getBounds();
// Iterate all prune clusters
var allClusters = pruneCluster.Cluster._clusters;
var clustersInView = [], clusterBounds;
for (var i = 0; i < allClusters.length; i += 1) {
clusterBounds = allClusters[i].bounds;
// Is this cluster visible? (within the visible bounds?)
if (boundsOfView.intersects([[clusterBounds.minLat, clusterBounds.minLng], [clusterBounds.maxLat, clusterBounds.maxLng]])) {
// Yes, visible, so store it
clustersInView.push(allClusters[i]);
}
}
// Now you have all your visible clusters in 'clustersInView'
它正在使用以下Leaflet方法:
getBounds()
获取地图可见部分的边界intersects()
,以测试聚类的边界是否与地图的边界相交(即可见或不可见)和PruneCluster:
bounds
(源代码中的第83行),其中包含群集的最小/最大坐标希望有帮助!