Google Maps with KML data

Some time ago I wrote about the introduction of a new feature for the Google Maps API: you can now use the same definitions file format as with Google Earth (technically speaking this is the KML 2.1 format).

All cool and neat, so I did a little experiment to try it out. And guess what: it didn’t work! No error messages, just my KML file was completely ignored.

Well, it appears that KML file must be accessible for Google to read and parse; in other words: it is not the client side API (Javascript) which reads the KML, but the Google service. Apparently what they do is: parse the file, calculate the correct view port and then send over all geo data, back to the client API.

As I hosted the file on my local computer (http://localhost/), this did not work. Duh!

This feature was not immediately obvious to me from the API documentation. And it might not be strictly necessary: as long as the KML file is hosted on the same server as the client HTML, the file could just be retrieved through XMLHttpRequest (the same origin policy would be satisfied). Performance might be an issue, though, as XML Parsing in the browser is not very efficient. And setting the viewport for the map would require another round trip and delay.

Conclusion: be sure to host your KML file such that it is accessible for Google (over http), otherwise it will just not work.

var geoXml = new GGeoXml('');