Newbie Event Listener Question

41 views
Skip to first unread message

pogue5

unread,
Feb 6, 2012, 9:39:01 AM2/6/12
to google-map...@googlegroups.com
Hi,

I am having what seems to be a simple problem but one that I can't seem to figure out. In this snippet of code, the info window does not display due to being undefined. It is only within the addListener event block where it is undefined but not elsewhere. Does anyone know why that would be the case?
contentArray is a global array that contains strings in each position

var markerCluster = new MarkerClusterer(map, markers,{gridSize: 20, maxZoom:8,  styles: styles[0]});
var infowindowarray = new Array();
for (var i = 0; i < markerArray.length; i++){

infowindowarray[i]= new google.maps.InfoWindow({
content: contentArray[i]});
google.maps.event.addListener(markerCluster, 'clusterclick',
function(cluster) {

if (map.getZoom() == 8) {
infowindowarray[i].open(map, cluster);
 }//if max zoom
 });
 
 }//for loop
       
 });
}

Thanks,

Greg

xelawho

unread,
Feb 6, 2012, 10:14:44 AM2/6/12
to Google Maps JavaScript API v3
it's generally recommended that you make one infowindow and change its
location and contents according to where the click took place.

I suspect that your code should look something like this, but if it is
a scope issue there's nothing like a good link to your map...

var iwindow = new google.maps.InfoWindow()
for (var i = 0; i < markerArray.length; i++){
google.maps.event.addListener(markerCluster, 'clusterclick',
function(event) {
if (map.getZoom() == 8) {
iwindow.setContent(contentArray[i])
iwindow.setPosition(event.latLng)
iwindow.open(map);
}
});
}

although presumably your markerArray length will be greater than the
number of markerClusters you have, which may explain the undefined
thing - I don't know if you can do something like for (var i = 0; i <
markerClusters.length; i++){ but I see that there is a
getTotalClusters() method - If I understand what you are trying to
do, maybe you should use that?

Rossko

unread,
Feb 6, 2012, 11:16:45 AM2/6/12
to Google Maps JavaScript API v3
> google.maps.event.addListener(markerCluster, 'clusterclick',
> function(cluster) {
> if (map.getZoom() == 8) {
> infowindowarray[i].open(map, cluster);
...
That defines a function to be run _when_ the event occurs.

Whatever your function is that includes that code ends, and local
variables in its scope go away.

Later on, the event occurs, your defined function is run, and
variables it requires are looked for in global scope. In this case,
they're not there.

pogue5

unread,
Feb 6, 2012, 11:17:45 AM2/6/12
to google-map...@googlegroups.com
Thank you for the response,

Do I need to set up my program so that a markercluster object is created for each cluster? ..Or can I pass the marker clusterer object all the markers on the map and see if it can organize it for me. I'm not sure how that works.

pogue5

unread,
Feb 6, 2012, 11:30:15 AM2/6/12
to google-map...@googlegroups.com
Thanks Rossko,

If these variables go out of scope, is there any way I can use the variables that are outside the function?

Rossko

unread,
Feb 6, 2012, 2:36:26 PM2/6/12
to Google Maps JavaScript API v3
> If these variables go out of scope, is there any way I can use the
> variables that are outside the function?

Sure. You can have them persist in global scope, or, you can have
them captured by the magic of 'function closure' at the time you set
up the listener.

Your approach here is bit flawed, there's little poiint in trying to
attach per-marker infowindow contents to a cluster that you don't know
how many or which markers are in it, and could change every time the
view changes.

But you seem to have started a parallel thread
http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/0f8cb86f2c45dd1e#
Listen for a click on a cluster, any cluster, and _then_ determine
what you want to display.
Reply all
Reply to author
Forward
0 new messages