1
votes

I am currently using the Google Maps MarkerClusterer v3 (http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/docs/reference.html) and have been very impressed with the functionality so far.

However, I now wish to add in an additional function to my map. When a user hovers over a list of the markers, the marker image is changed. This works great when the markers aren't clustered, but since I am also using the Clusterer, I need to be able to return the Cluster to which a specific marker belongs.

Does anyone know if this is possible? I checked the API documents, but couldn't find a method to return an array of the Clusters.

Essentially, here is a pseudo-code of what I need to do:

function changeClusterIcon(the_marker)
{
    var clusters = _CLUSTERER.getClusters();
    var clusters_length = clusters.length;
    var marker_pos = the_marker.getPosition().toString();

    for(var i = 0; i < clusters_length; i++)
    {
        var this_cluster = clusters[i];
        var the_markers = this_cluster.markers.length;

        for(var j = 0; j < the_markers; j++) 
        {
            var this_marker = this_cluster.markers[i];
            if(this_marker.getPosition().toString() == marker_pos)
            {
                return this_cluster;
            }
        }
    }

    return false;
}
2

2 Answers

2
votes

The MarkerClusterer library does not provide a way to retrieve the clusters. But there is an enhanced version of the library, MarkerClustererPlus, which provides more capabilities. Using MarkerClustererPlusapi-doc, you may use the MarkerClusterer.getClusters() function to retrieve an Array of the Cluster class instances. Then, you may use the Cluster.getMarkers() function to retrieve an Array of the markers that are within that Cluster. And with that, you should have what you need to move forward.

0
votes

Your function is almost correct, here the proper version:

function changeClusterIcon(the_marker)
{
var clusters = _CLUSTERER.getClusters();
var clusters_length = clusters.length;
var marker_pos = the_marker.getPosition().toString();

for(var i = 0; i < clusters_length; i++)
{
    var this_cluster = clusters[i];
    var the_markers = this_cluster.markers_.length;

    for(var j = 0; j < the_markers; j++) 
    {
        var this_marker = this_cluster.markers_[j];
        if(this_marker.getPosition().toString() == marker_pos)
        {
            return this_cluster;
        }
    }
}

return false;
}

So the markers property should be called as markers_ and the second foreach uses j instead of i.