javascript markerclusterer 检查标记是否在集群中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8245857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
markerclusterer check if marker is in cluster
提问by jaget
I have a web based map that is using the jquery-ui-mapand markerclustererplugin to make a google map.
我有一个基于 web 的地图,它使用jquery-ui-map和markerclusterer插件来制作谷歌地图。
I filter out which markers should be shown or not then update the map.
我过滤掉哪些标记应该显示或不显示,然后更新地图。
I need to create a list of unclustered markers and so to this end need a way to check clusters against markers and find out which are not clustered.
我需要创建一个未聚类标记的列表,因此为此需要一种方法来根据标记检查聚类并找出哪些未聚类。
Is there any techniques to do this?
有什么技术可以做到这一点吗?
I have tried to cycle through clusters and manually check the markers against clusters but get an error telling me the clusters property var_clusterer.clusters_
is not defined.
我试图循环遍历集群并针对集群手动检查标记,但收到错误消息,告诉我var_clusterer.clusters_
未定义集群属性。
回答by hitautodestruct
NOTE: This solution uses the MarkerClustererPluslibrary
注意:此解决方案使用MarkerClustererPlus库
You can use the getClusters() method to dish out an array of all the cluster objects currently being handled by MarkerClusterer.
您可以使用 getClusters() 方法来分发当前由 MarkerClusterer 处理的所有集群对象的数组。
var clusterManager = new MarkerClusterer( googleMap, markersArray, clusterOptions ); // setup a new MarkerClusterer
var clusters = clusterManager.getClusters(); // use the get clusters method which returns an array of objects
for( var i=0, l=clusters.length; i<l; i++ ){
for( var j=0, le=clusters[i].markers_.length; j<le; j++ ){
marker = clusters[i].markers_[j]; // <-- Here's your clustered marker
}
}
After you get the array using getClusters() loop through the cluster objects. For each cluster you can pull the current markers_
array and retrieve your clustered marker.
使用 getClusters() 获取数组后,循环遍历集群对象。对于每个集群,您可以拉取当前markers_
数组并检索您的集群标记。
getClusters() is now in the docs: MarkerClustererPlus docs
getClusters() 现在在文档中:MarkerClustererPlus docs
回答by crazy_p
NOTE:using MarkerClustererPlusv2.1.10
注意:使用MarkerClustererPlusv2.1.10
isMarkerClustered(marker: Marker, clusterer: MarkerClusterer): boolean {
const clusters = clusterer.getClusters();
for (let i = 0, l = clusters.length; i < l; i++) {
const markers = clusters[i].getMarkers();
for (const m of markers) {
if (m === marker && markers.length > 1) {
return true;
}
}
}
return false;
}
回答by dklt
A slightly dump, but effective method....
一个略显笨拙但有效的方法......
You may insert markers individually to a Marker Clusterer Object, and immediately (1)before and (2)after, call its .getTotalCluster() method to see if the newly added marker will go into a cluster.
您可以将标记单独插入到标记聚类器对象中,并在 (1) 之前和 (2) 之后立即调用其 .getTotalCluster() 方法以查看新添加的标记是否会进入集群。
I use this method, after getClusters() didnt work for me, maybe Im not using it via jquery.
我使用这种方法,在 getClusters() 对我不起作用之后,也许我没有通过 jquery 使用它。
var old_cluster_val = markerCluster.getTotalClusters(); // <-----(1)
markerCluster.addMarker( marker );
var new_cluster_val = markerCluster.getTotalClusters(); // <-----(2)
if (old_cluster_val == new_cluster_val) {
in_a_cluster.push(marker);
} else {
not_in_cluster.push( marker );
}