fleshyorgans

tryin’ not to be a linux fanboy

fleshyorgans Two Cats

How to add event listeners under the GMap2 API

August 29th, 2006 · No Comments

Some Javascript stuff here concerning Google Maps and coding for them.

This took me a little bit to figure out, cause I was a little slow, last night. Also, the Google Map API documentation doesn’t seem to cover this sort of eventuality.

Say that, for whatever reason, you have an array of GMarker objects and you want to programatically apply an info window to each of them.

Doing so is not as straightforward as this code:

for (i in markers) {
...
GEvent.addListener(markers[i], "mouseover", function() {
marker[i].openInfoWindowHtml(text_var);
} );
}

This won’t work, because in the third parameter, “i” is passed in literally. In order to have the “i” successfully expanded to an index, you have to eval the function creation, like so:

for (i in markers) {
...
GEvent.addListener(marker[i], "mouseover", eval('function() {' +
'marker[' + i + '].openInfoWindowHtml("' + text_var + '"); ' +
'} ')
);
}

Just passing it on.

Tags:

0 responses so far ↓

  • There are no comments yet...Kick things off by filling out the form below.

Leave a Comment