Using XSL to convert a KML file to a static Google Map image
Maps provided by Google can be embedded into a website using three approaches. The first approach consists in simply creating a custom map via the custom map editor. It allows you to add placemarks, draw lines and shapes, and publish your map for public consumption. The map can be then embedded in your website.
The third option then consists in relying on static images to deliver maps. Google provides such a functionality via the Static Maps API. This service creates a map based on URL parameters sent through a standard HTTP request and returns the map as an image that can be displayed on the web page. Similarly to custom maps, placemarks and lines can be added via additional URL parameters.
A common scenario is to create and publish a custom map, and choose to convert it later to a static image for an easier consumption by mobiles. Unfortunately, there is no built-in mechanism to convert a custom map to an equivalent static map. The solution starts by downloading the KML file of your custom map. As KML is based on the XML standard, it can be processed by a XSL transformation to generate a static URL with a format compatible with the Google Static Maps API. </p>
To illustrate the process, I have created a custom map listing the places where I worked. I have then downloaded the related KML file. The structure of this file is easy to follow as it is a collection of placemarks defined by a name, a set of coordinates, and a style. In the following excerpt, the placemark “Boca Raton” is associated to style 9 which is, in turn, rendered by a red dot.
I have included below an example of a XSL transformation which identifies all the placemark tags and produces the final image URL. For each of these tags, the related style is located and the final color of the marker is extracted. The first letter of the name of the placemark is used as the label in the final transformation. The XSL transformation can be adapted to accommodate more complex scenarios such as the rending of lines or shapes.
The end result follows. The first map is created using the Google Static Map API, while the second map is simply created by embedding the custom map directly in the page.
Google Map defined in a HTML frame