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:

<config>
<title>Google Maps GPS Track</title>
<key>YOUR GOOGLE MAPS KEY</key>
<width>540px</width>
<height>540px</height>
<units>mi</units>
<colinear-threshold>0.0001</colinear-threshold>
<icon>var icon = new GIcon();
icon.image = "http://www.example.com/images/x.png";
icon.shadow = "http://www.example.com/images/xshadow.png";
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>
</config>

The configuration parameters are:

title

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

key

This is your Google Maps key.

width

The width of the map.

height

The height of the map.

units

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.

colinear-threshold

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.

icon

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.]

Comments

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 #
Nice program. A few simple improvements you could consider:

1) Start the html page with

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
so that browser can parse it in standard compliance mode.

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

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
  <head>
    <style type="text/css"> v:\* { behavior:url (#default#VML); } </style>
    <title>title</title>
... that could/should be strict but I am now too exhausted after entering html tags to this message to edit the above further...
—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 #