This web site is no longer maintained and may be outdated. It is retained primarily for historical reasons.

Deploy your own slippy map

schermafdruk met kaart

From time to time I do some long-distance walking and multiple days of trekking. Most of the time, I carry my GPS with me, allowing me to keep track of the route I have walked. Sometimes I want to share the route with other people. Printing this route on top of Openstreetmap map is nice. This article describes a way to do this.

Openstreetmap and your GPX file

This is a GPS track of 2009’s Rode Kruis Bloesemtocht.

Over at there is a description on how to add a GPX track on an OSM map and embed this map onto a webpage: OpenLayers Simple Example. Apart from the lenghty Javascript you need to add to a webpage, additional problems include the necessity to specify the centering coordinates and zoomlevel yourself each and every time.

With some customizations, embedding this map on this page doesn’t take more than a single <iframe> tag. You may include multiple <iframe> tags to include embed multiple maps with different GPX tracks each. How I do this?

How to do this?

This page includes a single <iframe> tag to include the file that actually takes care of loading all the required components. A GET variable is used to tell the which GPX file needs to be loaded and rendered. The line in the source of this webpage:


The file osm-gpx.php returns the actual <div> that contains the map, provided the GPX file that was requested has a valid syntax and is readable:

if (isset($_GET['gpx'])) {
        if (preg_match('/^[a-z0-9-.]+\.gpx$/i', $_GET['gpx'])) {
                $gpx = $_GET['gpx'];

if (isset($gpx) && is_readable($gpx)) { ?>
          <script src=""></script>
          <script src=""></script>
          <script src="./topo/osm-gpx.js"></script>
         <body onload="init('<?php echo $gpx;?>');">
          <div style="width:100%; height:100%" id="map"></div>
<?php } else { ?>
          <link rel="stylesheet" media="screen" type="text/css" href="/css/site-regular.css">
          <p>No valid GPX file provided.</p>
<?php } ?>

The file loads three Javascript files. One of the files is from, one from and a local one. The contents of the local file:

var map;

function init(gpxfile) {
	map = new OpenLayers.Map ("map", {
		maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
		maxResolution: 156543.0399,
		numZoomLevels: 19,
		units: 'm',
		projection: new OpenLayers.Projection("EPSG:900913"),
		displayProjection: new OpenLayers.Projection("EPSG:4326")
	} );

	layerMapnik = new OpenLayers.Layer.OSM.Mapnik("Mapnik");

	var lgpx = new OpenLayers.Layer.GML("GPS route", gpxfile, {
	    format: OpenLayers.Format.GPX,
	    style: {strokeColor: "red", strokeWidth: 5, strokeOpacity: 0.5},
	    projection: new OpenLayers.Projection("EPSG:4326")

	if( ! map.getCenter() ){'loadend', map, function(){this.zoomToExtent(lgpx.getDataExtent())});
		map.setCenter(null, null);


Basically, that’s it. Just upload the GPX file to your webserver, point to it from the <iframe> and you are done. Or if you want to have it even more easier, just provide the track.

Use the source, Luke!

From time to time people are asking me on details of my implementation. Here is a tarball of the files I am using. I love to hear your experience.