Follow me on Twitter:

Display current location on your web page using the SPOT GPS tracker

Posted: August 3rd, 2011 | Author: | Filed under: Tricks & Tips | Tags: | 1 Comment »

 

UPDATE: Ignore all the crazy shit below. Just use this script:

https://gist.github.com/manos/8359450

It fetches your tracks and saves them in a .json file, as well as a plain text file containing the last latitude,longitude. The below stuff may still be helpful for how to get a Google map displayed on your site… there’s got to be better ways though 🙂

The SPOT GPS tracker is not only a lifesaver, but also a handy tool for motorcycle (ok, and other types) travelers. If you subscribe to the Track Progress service, you can tell the GPS device to send your location to SPOT via satellite every 10 minutes, and then export those tracks at a later time.

They even have an API from which you can fetch an XML document with all your current tracks! The unfortunate part, and the reason for this post, is that they only keep 30 days of GPS coordinates. It’s not a problem if you export your data to Spot Adventures and create an “adventure” — that will live forever. But if you wish to present “my current location” on your personal web page, for example, you’re out of luck. It will only work as long as you’ve used the track progress functionality within the last 30 days.

I guess you need to cache the last used location yourself. OK, this shouldn’t be hard. This is how to do it with a little perl script (download here, see in action in CharlieTracker here):

#!/usr/bin/perl
 # Author: Charlie Schluting <[email protected]> (c) 2011
 #
 use XML::Simple;
 #use strict.. heh, no, this barely works.</code>
 $CACHEFILE = "/home/charlie/lastspotlocation.txt";
 $XML = "/home/charlie/spot.xml";
 $JS = "/stash/www/charlierides.com/files/map.js";

The CACHEFILE is where I store the last known latitude and longitude. It’s mostly for reference, so that other things (aside from this script) can use it.

XML is the location I store the fetched XML from SPOT’s API.

JS is the file I write the javascript out to. OK, you probably don’t need to care about this part of the script.. but here’s the details: if you use joomla or wordpress, you might not want to enable PHP code execution or other evil things like that. So, in order to include a snippet of generated HTML, the only choice (as far as I know, aside from editing the DB where a “display html” module stores its data), is to write javascript to include another javascript file. So that’s what I do.. the script actually generates javascript that when run, will spit out html. If you don’t care about that and just want the map part, read on.

# hahaha, oh man..
 `wget -q -O $XML http://share.findmespot.com/messageService/guestlinkservlet?glId=0Vn4kA4MiPgNSYD52NgPjuVJDpUCSUlGW`;

This part fetches the XML file from spot, using my shared page identifier.

# create object
 $xml = new XML::Simple;
 # read XML file
 $data = $xml->XMLin("$XML");

# this is how we overcome spot's API not keeping >30 days. If they've aged out, do nothing (i.e. keep using the old data).
 die("No messages found, totalCount is 0, ABORTING LIKE AN UGLY KID") unless $data->{totalCount} > 0;

# the first object is always the most recent:
 $lat = $data->{message}->[0]->{latitude};
 $long = $data->{message}->[0]->{longitude};

# just because (hey, what if something else wants to use this?)
 open(FILE, ">$CACHEFILE");
 print FILE $lat . "," . $long . "\n";
 close(FILE);

And the rest (above) is pretty self explanatory. Using just this part of the script, you’ve overcome the annoying limitation that is SPOT Track Progress (losing your last known location). Your last known coordinates will be in CACHEFILE. For completeness, I will include the rest of this horrible hack I used:

# ugly shit that writes out javascript to write out html, to include a linked static google maps image
 $googleoptions = "&zoom=8&size=140x152&sensor=false&maptype=hybrid";
 $googlelink = "<a target=\"blank\" href=\"http://maps.google.com/maps?q=" . $lat . "," . $long . "+(charlie)&z=8&t=h\"\>";
 $header = "<p>Current location (since the last GPS update):</p>";
 $js = 'document.write(\''. $header .'\');' . '
 document.write(\''. $googlelink .'\');' . '
 document.write(\'<img src="http://maps.googleapis.com/maps/api/staticmap?center=' . $lat .
 ',' . $long . $googleoptions . '" />\');
 document.write(\'</a>\');
 document.write(\'<p>Or <a href="http://share.findmespot.com/shared/faces/viewspots.jsp?glId=0Vn4kA4MiPgNSYD52NgPjuVJDpUCSUlGW"
 target=blank>view all recent tracks.</a></p> \');
 ';
 open(FILE, ">$JS");
 print FILE $js;
 close(FILE);

So, what this writes out (in HTML, finally), is an IMG tag embedding a static google map (because I’m using a small thumbnail), which links to the google maps page when clicked:

Map: http://maps.googleapis.com/maps/api/staticmap?center=45.42553,-122.52251&zoom=8&size=140×152&sensor=false&maptype=hybrid
Link target: http://maps.google.com/maps?q=45.42553,-122.52251+(charlie)&z=8&t=h

The parameters you can pass google maps are well documented, so I don’t need to rehash them here. You may even prefer to use the real maps API, rather than the static image one. That too, is well documented.

Run this from cron every 15 minutes, and you’ll always have your last checked-in GPS coordinates!


1 Comment »

One Comment on “Display current location on your web page using the SPOT GPS tracker”

  1. 1 Thanks said at 08:29 on June 28th, 2012:

    Great Post


Leave a Reply

  •