GPX Google

Volume 9, Issue 61; 20 Jun 2006; last modified 08 Oct 2010

This is the perl script that I use to build Google Maps of my rides around Amherst. [Update: supports more variance in the GPX format.]

When I first published Every street in Amherst, several people asked me how I was generating the map data.

I expect other tools to do this exist, but I thought it would be nice to share, so I cleaned mine up a bit. A bit. It's still rough around the edges, but I'm not likely to improve it further.

My tool is gpxgoogle. It reads one or more GPX track files and produces an HTML document that will display those tracks on a Google Map.

You must configure gpxgoogle with a small XML file in your home directory: ~/.gpxgoogle:

<title>Google Maps GPS Track</title>
<icon>var icon = new GIcon();
icon.image = "";
icon.shadow = "";
icon.iconSize = new GSize(3, 3);
icon.shadowSize = new GSize(3, 3);
icon.iconAnchor = new GPoint(1, 1);
icon.infoWindowAnchor = new GPoint(2, 0);</icon>

The configuration parameters are:


This will be used as the title of the generated HTML page.


This is your Google Maps key.


The width of the map.


The height of the map.


The units that you wish to use for displayed track data. You can specify “mi” (miles), “km” (kilometers), “ft” (feet), or “m” (meters).

I've tried reasonably hard to make the calculations accurate, but of course they're not. They don't account for irregularities in the terrain, inaccuracy in the GPS, or the limitations of the floating point library.


In an effort to reduce the number of points on the map, gpxgoogle removes “colinear” points. These calculations treat the map as completely flat and the latitudes and longitudes as rectangular coordinates. This is wrong, of course, but for the distances in a typical track, I don't think the errors are significant.

Anyway, if the discrepancy between the point that is exactly colinear between two points and the actual position of the point in between is less than this threshold, the middle point is discarded. Empirically, a value of 0.0001 seems to work pretty well.


The points along the track are identified with an icon. The content of this element must be a fragment of Javascript that declares a variable named “icon” and initializes it as appropriate for a Google Maps GIcon.

Obvious improvements and open questions

As I said, I'm probably not going to do much more with gpxgoogle in the short term. But when (or if), I find the time, there are a few obvious improvements to be made.

  1. Support the configuration parameters as command line options.

  2. It's pretty clear that for large numbers of points, the techniques I'm using don't work well, they cause the Google Maps Javascript to run too long. I don't know if there are better ways to use the Google Maps API.

  3. It'd be nice to be able to generate Yahoo maps too.

  4. These maps don't work in PDF at all. Can anything be done about that?

If you decide to pursue any of these improvements, or any others, please let me know. Whether you do or not, share and enjoy!

[Update 21 June 2006: Not using version 2 of the Google Maps API was clearly short-sighted. Now updated. But not (yet) using any of its new features.]

[Update 12 July 2006: It appears that GPX tracks don't always include a timestamp for each point, as I'd expected. That caused a division by zero error, which I've now fixed.]


As far as I can see, neither the AJAX nor the Flash Yahoo Maps APIs have any support for tracks, so maybe that's off the table too.

—Posted by Norman Walsh on 21 Jun 2006 @ 01:33 UTC #

1) Start the html page with

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

2) Support Internet Exploder, by continuing the page as:

<html xmlns="" xmlns:v="urn:schemas-microsoft-com:vml">
    <style type="text/css"> v:\* { behavior:url (#default#VML); } </style>
—Posted by Tomi Ollila on 27 Apr 2007 @ 06:33 UTC #

Using your gpxgoogle for some years now.
Very happy with it.

You don't have a V3 version by any change?
I was looking into converting the pages but without much succes.

—Posted by Rudy on 27 Sep 2011 @ 01:46 UTC #

I do, actually, I think. I'll try to post an update soon.

—Posted by Norman Walsh on 27 Sep 2011 @ 02:16 UTC #
—Posted by Norman Walsh on 18 Oct 2011 @ 03:29 UTC #