On Thursday 19 February 2009 10:41:58 you wrote:
> On Thu, Feb 19, 2009 at 5:32 PM, Daniel Benoy <dan...@benoy.name> wrote:
> > In short, that script calls nsupdate which, using some private key 
> > authentication magic, changes my 'myphone.dynamic.example.com' address to 
> > the IP of my phone's ppp0 interface.  So every time I log in to GPRS I 
> > become reachable by that address.
> >
> > Then, I made up a short script on my blog that creates an outgoing 
> > connection to the gpsd port on my phone's address, and asks it where its 
> > current location is, then renders the results using the google maps API 
> > (which is free to use non-commercially).
> 
> Nice!
> 
> Mind sharing this blog script, too?
> 
> r
> 

----
<?php
// Read in the line from cache
$fresh = 0;
$cachefile = '/var/cache/gps';
$fh = fopen($cachefile, 'r');
if ($fh) {
  $resp = fread($fh, filesize($cachefile));
  fclose($fh);
}

// Refresh from GPS only if more than a minute has passed since the creation 
time of the cache
if (time() - filemtime($cachefile) > 60) {
  print "Refreshing from live GPS data ... ";
  for ($i=0; $i<3; $i++) {
    $sock = @fsockopen("myphone.dynamic.example.com", 2947, $errno, $errstr, 3);
    if ($sock) { break; }
  }
  if (!$sock) {
    print "Could not connect to GPS.  Using cached data.<br/>";
  } else {
    for ($i=0; $i<10; $i++) {
      @fwrite($sock, "O\n");     # Query what we actually want
      $realresp = @fread($sock, 384);
      if (strncmp($realresp, 'GPSD,O=', 7)) {
        print "<!-- Unexpected response from GPS connection. -->";
      } elseif (strncmp($realresp, 'GPSD,O=?', 8) == 0) {
        print "<!-- The GPS connection responded, but had no fixed location. 
-->";
      } else {
        $resp = $realresp;

        $fh = fopen($cachefile, 'w');
        if ($fh) {
          fwrite($fh, $resp);
          fclose($fh);
          $fresh = 1;
        }
        print "Success!<br/>";
        break;
      }
    }
    if ($i == 10) {
      print "Failed.  Using cached data<br/>";
    }
    @fclose($sock);
  }
}

//print($resp);
?>

<script 
src="http://maps.google.com/maps?file=api&v=2&key=MY-GOOGLE-API-KEY-HERE-GET-YOUR-OWN-FROM-GOOGLE";
 type="text/javascript"></script>

<div id="map" style="width: 500px; height: 300px"></div>
<script type="text/javascript">
  //<![CDATA[
    if (GBrowserIsCompatible()) {
      var map = new GMap2(document.getElementById("map"));
      map.addControl(new GSmallMapControl());
      map.addControl(new GMapTypeControl());
    }
  //]]>
</script>

<?php
// Make sure the data is fresh
if ($fresh || time() - filemtime($cachefile) < 900) {
  // Parse GPS response
  $location = split(" ", $resp);
?>

  <script type="text/javascript">
    //<![CDATA[
      if (GBrowserIsCompatible()) {
        map.setCenter(new GLatLng(<?php print($location[3])?>, <?php 
print($location[4])?>), 13);
        var point = new GPoint(<?php print($location[4])?>, <?php 
print($location[3])?>);
        var marker = new GMarker(point);
        map.addOverlay(marker);
      }
    //]]>
  </script>

  Last update (GPS time): <?php print(date("Y-m-d H:i:s", $location[1]))?><br/>
  Latitude: <?php print($location[3]); ?><br/>
  Longitude: <?php print($location[4]); ?><br/>
  Heading: <?php print($location[8]); ?><br/>
  Ground Speed (km/h): <?php print($location[9]  * 3.6); ?><br/>
<?php } else { ?>
  <script type="text/javascript">
    //<![CDATA[
      if (GBrowserIsCompatible()) {
        map.setCenter(new GLatLng(0, 0), 1);
      }
    //]]>
  </script>

  <p>Unfortunately, there has been no GPS data update in at least 15 minutes.  
This probably means that the phone is currently unreachable.</p>
  <p>The phone must be connected to a publicly accessible network, such as 
cellular internet GPRS, in order to be reached for GPS location information.   
If the phone is connected to a local wireless LAN which is firewalled, or the 
phone is off or its GPS receiver is deactivated, then it will be unable to 
provide telemetry.</p>
<?php } ?>
----

-- 
Daniel Benoy
http://daniel.benoy.name

_______________________________________________
Openmoko community mailing list
community@lists.openmoko.org
http://lists.openmoko.org/mailman/listinfo/community

Reply via email to