Using OpenStreetMap Overview Part 2: JavaScript APIs
Saturday, August 23rd, 2008
After obtaining the map data the next step is to actually use it via a JavaScript library providing standard features such as lines, markers and click/drag functionality as offered by Google Maps.
There are currently 3 main ways to use OpenStreetMap data:
- Google Maps API - However this would introduce licensing issues which were likely the reason OpenStreetMap data was chosen in the first place.
- OpenLayers - An open source Google Maps type API.
- OpenLayers via Mapstraction - The advantage of Mapstraction is that you write code once, and can then easily switch it between one of 9 major mapping providers including Microsoft, Yahoo, Google, MultiMap.
In theory using Mapstraction is an obvious choice leading to a much more portable application. In practise however things are not so simple. I found myself frequently viewing the Mapstraction JavaScript code to find undocumented functions (use of which is not recommended by the documentation). Also unless your application is very simple “if” statement will be needed in various parts of your code that call the Map APIs directly where Mapstraction does not provide the required functionality.
Even with undocumented functions, and “if” statements in the code supporting 3 mapping providers in places I found it necessary to go a step further and actually override Mapstraction functions where it was simply not possible to do what was required otherwise. I am now experiencing an issue whereby markers placed on the map move when changing zoom levels which I have not yet found a solution for. Same code works fine when switched to Google or Multimap. Therefore unless you have a very simple application or you really know what you are doing with JavaScript, and think the benefits of Mapstraction outweigh the extra time spent coding for it, simply using OpenLayers directly may be the best choice.
Using OpenLayers with OpenStreetMap
The process for using OpenLayers is relatively straight forward however involves a number of steps.
- Download OpenLayers
- Copy the OpenLayers “build/OpenLayers.js”, and “theme/”, “img/” directories to your webserver, putting them in the same directory (also be sure not to rename OpenLayers.js / change the letter case on the filename or it will not run correctly).
- Copy the OpenStreetMap JavaScript file to your webserver.
- Copy the OpenStreetMap HTML example script found on the Wiki to your webserver.
- Edit the HTML example script such that the paths to the “OpenLayers.js” and “OpenStreetMap.js” JavaScript files point to your local copies.
- Edit the “OpenStreetMap.js” JavaScript file changing the tile location to be the path where you uploaded your map files to.
Or download a zip file with the above already completed, you just need to the edit the tile location path in “OpenStreetMap.js”.
Then see the OpenLayers documentation and Examples for details on adding markers lines etc to the map.
Also see Part 1: Obtaining mapping Data.

To convert the XML .osm files into .png image files that can be displayed by the web browser a
Kosmos may occasionally crash while starting up, this is generally due to someone editing the “
Google used to provide a SOAP based API that allowed programmatic access to Google search results. It has since been discontinued, however those that obtained keys prior to December 2006 can still use them.