[英]markerclusterer check if marker is in cluster
I have a web based map that is using the jquery-ui-map and markerclusterer plugin 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_
。
NOTE : This solution uses the MarkerClustererPlus library 注意 :此解决方案使用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. 使用getClusters()循环通过集群对象获取数组后。 For each cluster you can pull the current
markers_
array and retrieve your clustered marker. 对于每个群集,您可以拉出当前的
markers_
array并检索您的聚簇标记。
getClusters() is now in the docs: MarkerClustererPlus docs getClusters()现在位于docs: MarkerClustererPlus docs中
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. 您可以将标记分别插入Marker Clusterer对象,并在(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 );
}
NOTE: using MarkerClustererPlus v2.1.10 注意:使用MarkerClustererPlus v2.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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.