Sunday, 3 April 2011

Google Maps Data Files

The My Maps feature in Google Maps is really handy for planning out a quick route or just seeing how far it is from one place to another. You can also drop on various place markers, add text to them and also add photos as I’ve done, for example, with my City of London Boundary dragons map.

As well as storing all the maps in the My Maps section of Google Maps you can also download any of them as a .kml file, which contains XML describing the map features, waypoints, etc. in the Keyhole Markup Language. The download option is available when you are editing a map and is accessed via the option View in Google Earth, which becomes available on the toolbar on the right as soon as you start editing. Clicking the option downloads a KML file.

The problem is that the .kml file doesn’t really have much data in it. It has the name and the description of the map, and the name and description of any map lines but the map data is not downloaded in the file. Instead it is left in Google Maps and the .kml file contains the URL to the map data in a <link> node.

While this abbreviated, deferred content is quite suitable for Google Earth’s consumption, if you were hoping to upload the map to some other mapping application the chances are it won’t get you very far. Typically, mapping applications expect to find the data in the .kml file.

Fortunately we can resolve this little issue by looking at the URL that Google My Maps offers for downloading the .kml file in the first place. Let’s take a look at the links available, using that Boundary Dragon map as an example.

Firstly, looking at the link offered to you if you ignore the Google Earth viewing option, but just click the Link option on the toolbar. The link it offers to paste into IM or email looks like this (with various bits of not-strictly-necessary junk taken out, for brevity):

http://maps.google.com/maps/ms?msa=0&msid=203846768851680872761.00046ae298586b10c2e49

Now let’s compare with what link is executed when you click on the View in Google Earth toolbar option (again, sanitised for easy viewing):

http://maps.google.com/maps/ms?msa=0&msid=203846768851680872761.00046ae298586b10c2e49&output=nl

As you can see, there’s an extra value added into the link specifying that the Google Maps server should output a file containing a network link (which appears to be what NL stands for). If you now look at the link contained in the .kml file it looks (after cleaning up) like this:

http://maps.google.com/maps/ms?msa=0&msid=203846768851680872761.00046ae298586b10c2e49&output=kml

If you paste this link into your browser’s address bar it will download another .kml file with full data inside it.

So, the answer is to take the original URL in the View in Google Earth hyperlink and replace the =nl with =kml. Easy.

Alas this does not work if you try and use =gpx – Google Maps is not interested in supporting the GPX format, commonly used by many mapping sites, however Google Earth is happy enough to load up GPX files.