GPX Google
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 MapsGIcon
.
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.
-
Support the configuration parameters as command line options.
-
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.
-
It'd be nice to be able to generate Yahoo maps too.
-
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.
1) Start the html page with
2) Support Internet Exploder, by continuing the page as:
strictUsing 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.
I do, actually, I think. I'll try to post an update soon.
Updated.