google.maps.event.trigger and MarkerClusterer

2,180 views
Skip to first unread message

Martin Stjernegaard

unread,
Nov 17, 2010, 1:31:17 PM11/17/10
to Google Maps JavaScript API v3
Hi guys

I'm working on a project where I need to implement the MarkerClusterer
for the markers on the same location. That is already done, and big
thanks to the people who created the MarkerClusterer.

So what I want to do next is to open up an infowindow on the clusters
that displays a list with the titles of the markers in the cluster.
These titles needs to be clickable and open up the infowindow for the
clicked marker. It is all showing up nicely, but the problem I'm
facing is that i'm getting an error telling me that "clusterMarkers is
not defined" when I click the links.

I have seen elsewhere others using this piece of code to do the job,
but somehow I can't make it work:
google.maps.event.trigger(clusterMarkers[" + i + "], 'click'

Here's my code so far:

var markerCluster = new MarkerClusterer(map,allMarkers, {gridSize: 5,
zoomOnClick: false});
var clusterMarkers = [];

// Listen for a cluster to be cliced
google.maps.event.addListener(markerCluster, 'clusterclick',
function(cluster) {

var content = '';

// Get info about the clustered markers
clusterMarkers = cluster.getMarkers();

for(i=0; i<clusterMarkers.length;i++){
content += "<a href=
\"javascript:google.maps.event.trigger(clusterMarkers[" + i + "],
'click')\">" + clusterMarkers[i].title + "</a><br />";
}

// Convert lat/long from cluster object to a usable MVCObject
var info = new google.maps.MVCObject;
info.set('position', cluster.center_);

var infowindow = new google.maps.InfoWindow();
infowindow.close();
infowindow.setContent(content);
infowindow.open(map, info);

I'm sorry that I don't have the project online so you can see it but I
hope you can help me out anyway.

Big thanks

/Martin


CroNiX

unread,
Nov 17, 2010, 10:53:42 PM11/17/10
to Google Maps JavaScript API v3
most likely it is an issue of scope due to where you are initially
declaring
var markerCluster = new MarkerClusterer(map,allMarkers, {gridSize: 5,
zoomOnClick: false});

It needs to be global.

try doing:
var markerCluster = null; at the very TOP of your code before you do
anything else. Most likely you are declaring your map variable
somewhere like this needs to be done.

then later where you have the
var markerCluster = new MarkerClusterer(map,allMarkers, {gridSize: 5,
zoomOnClick: false});

just remove the "var" and change it to:
markerCluster = new MarkerClusterer(map,allMarkers, {gridSize: 5,
zoomOnClick: false});

I think it will work then.
Reply all
Reply to author
Forward
0 new messages