markerclusterer check if marker is in cluster

江枫思渺然 提交于 2020-03-21 21:54:58

问题


I have a web based map that is using the jquery-ui-map and markerclusterer plugin to make a google map.

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.


回答1:


NOTE: This solution uses the MarkerClustererPlus library

You can use the getClusters() method to dish out an array of all the cluster objects currently being handled by 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() is now in the docs: MarkerClustererPlus docs




回答2:


NOTE: using 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;
}



回答3:


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.

I use this method, after getClusters() didnt work for me, maybe Im not using it via 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 );
}


来源:https://stackoverflow.com/questions/8245857/markerclusterer-check-if-marker-is-in-cluster

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!