Google maps and clustering markers

You can do really nice things with the Google Maps API. Even more when you use the gmaps-utility-library.
Difficulty is that the documentation is not that great of the extra tools.

In my case I wanted to do someting special with my markers (and my clustered markers). When there is an alert at the location my markers should turn red instead of the default green.
The cluster markers does have the ability to set a different style set. Problem is that the logic in the cluster marker is that it changes the style based on the amount of markers in the cluster.

I incidentally found and used the correct version of the add-on. I found it at Google code. While writing this post I re-googled the add-on so that I could link to the site. Like so.
I discovered that that version had exactly what I missed: a parameter for the calculator function. Thinking I had an outdated version I copied this version and rewrote the code.
It broke. It seems it was voor Google Maps API v2, and I was using V3.
Then I discovered that there are more versions of the add-on library! The new version for V3 did not have a parameter for specifying a function to calculate what style I wanted to use.

Eventually I just overridden the function as so:

MarkerClusterer.prototype.calculator_ = function(markers, numStyles) {
    var index = 1;
    var count = markers.length;

    for (var i = 0; i < markers.length; i++) {
        if (markers[i].alert != undefined && markers[i].alert) {
            // show alert
            index = 2;
            break;
        }
    }

    return {
        text: count,
        index: index
    };
};

When I create a marker I add the

alert

property before adding it to the array that I pass on to the MarkerClusterer. It works like a charm!

It would be nicer (and cleaner) to specify the function when initializing the MarkerClusterer, but I am just glad I got it working like I wanted to.

Leave a Comment

You must be logged in to post a comment.