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.
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.
3 Answers
Reset to default 9NOTE: 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
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;
}
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 );
}