Tuesday, February 23, 2010

GWT and Google Maps - Opening an Info Window on a Marker

I am using the Google GWT APIs library to use maps on www.targetrooms.com

Opening the info window at a certain location worked fine but when I tried to open it over a marker an error was thrown.

This works:

map.getInfoWindow().open(latLng, content);

But this:

map.getInfoWindow().open(marker, content);

throws this:

com.google.gwt.core.client.JavaScriptException: (TypeError): b is
undefined
fileName: http://maps.gstatic.com/intl/en_ALL/mapfiles/193c/maps2.api/main.js
lineNumber: 1224

By using FireBug to break on the exception I found that the maps were looking for an anchor point for the info window:

line 1224: b=a.infoWindowAnchor

So by setting an anchor point on the markers icon before using it like so:

icon.setInfoWindowAnchor(Point.newInstance(10, 10));

MarkerOptions options = MarkerOptions.newInstance(icon);

Marker marker = new Marker(latLng, options);

the info window now opens as expected.