Javascript: Google MarkerCluster - Stop the first marker from clustering

There is a mobile optimized version of this page, view AMP Version.

Recently I have been working on a project for work that allows customers to book train tickets and a hotel in one package (saving them money), how the system is working is the user first selects there train travel, after which they are shown all the hotels on a Google Map. This map also shows the destination train station which is automatically selected.

The problem was that due to the possible amount of hotels on the map I was using a plugin called MarkerCluster, this neat little plugin will cluster near-by markers into one marker (numbered with the amount of markers within). The problem though was that the train station was also clustered, which was undesired as when you zoom out you cannot now see where the train station was! (Also on load if there were a bunch of markers around the train station you would not see the train station InfoWindow.

To get around this is relatively simple, just before I send the markers array to MarkerClusterer I remove the first marker (knowing that the first marker placed on the map is the train station).

So this is how you would normally setup MarkerClusterer onto your Google Map:
(using: Google Maps API 3jQuery Ui MapsMarkerClusterer)

$(this).gmap('set', 'MarkerClusterer', new MarkerClusterer(map, $(this).gmap('get', 'markers'), mcOptions));

And to remove the first marker so that it is not included in clustering do this:

objA = $(this).gmap('get', 'markers');
objA.splice(0,1);
$(this).gmap('set', 'MarkerClusterer', new MarkerClusterer(map, objA, mcOptions));

Please comment if you have any questions, suggestions, comments or etc. I will be posting again soon with perhaps a solution to group multiple in exclude using a smarter approach.

Author: Dean Williams

I'm a Web Developer, Graphics Designer and Gamer, this is my personal site which provides PHP programming advice, hints and tips