Markerclusterer checks if a marker is in a cluster

I have a web map that uses jquery-ui-map and markerclusterer to make a google map.

I filter out which markers should be shown or not, and then update the map.

I need to create a list of non-clustered tokens, and so for this I need to check the clusters against the tokens and find out which ones are not clustered.

Are there any methods for this?

I tried to loop through the clusters and manually check the tokens against the clusters, but get an error message indicating that the var_clusterer.clusters_ cluster var_clusterer.clusters_ is undefined.

+4
source share
3 answers

NOTE This solution uses the MarkerClustererPlus library .

You can use the getClusters () method to collect an array of all the cluster objects that are currently being processed 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 your clustered marker } } 

After you get the array using getClusters () iterate over the objects in the cluster. For each cluster, you can get the current markers_ array and get your cluster marker.

getClusters () is now in the documentation: MarkerClustererPlus docs

+7
source

A bit of a dump, but an effective method ....

You can insert markers separately into the cluster cluster object and immediately (1) before and (2) after calling your .getTotalCluster () method to see if the newly added marker will fall into the cluster.

I use this method after getClusters () does not work for me, maybe I do not use it through 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 ); } 
+2
source

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; } 
+2
source

Source: https://habr.com/ru/post/1382737/


All Articles