Oct 19, 2008

Google Maps API: Tips and Tricks

Google Maps API is awesome! There is no doubt about it. The Javascript functionality allows developers to do anything and everything when it comes to geo positioning. As I've incorporated the maps API into the gt project (release coming soon), I ran into a few issues that require some tricks in order to work. Here is the list:

  1. Never use addOverlay before you call setCenter. If you do, you will experience the "Cannot call 'getProjection' on null" error which results from Google Maps trying to place a marker on a map that does not exist.

  2. Attach to events in a separate function. In one of the map API examples I found the nice functionality to pop a message box when the user clicks on a marker. This is done through the GEvent.addListener call. The problem comes when addListener and addOverlay are called within the same function. This causes Google Maps to always display the same pop-up at the same location regardless of which marker has been clicked.

More tips and tricks to come at a later point. :)


Pamela Fox said...


Interesting project (gt).

Re 2 - That's a general issue in JS called function closure. We have documentation on that here:

It's a common issue in JS when attaching listeners in a loop... but I also ran into it first when using the Maps API.

Unknown said...

interesting... thanks for the comment!

I've also found this additional info on Javascript closures: