Location Sensitive Programming

Jun
17

As we move into a world where social media is king we are inching closer to a change in paradigm where no longer will we need to say where we are, but our browsers will be able to tell a web application where we are (if we let it) and we can then be served content based upon this. With the iPhone OS 3.0 update it will be possible for the Safari browser to access the longitude and latitude information provided by the phone's built in GPS, in a web browser using JavaScript.

Fortunately this new feature is based on the W3C Geolocation API specification as is the Geode extension for Firefox meaning compatibility with other devices. The syntax for getting the location can be seen in the following example.

if (navigator.geolocation) {
   navigator.geolocation.getCurrentPosition(successCallback,
          errorCallback,
          { maximumAge:600000, timeout:0 });
}

This means you will need to create two functions - one to get the latitude and longitude, and another to perform error handling if they're not available. What we are doing here is we are testing to see if the the browser supports the geolocation namespace, if it does then we're asking the browser to give us the current position and to pass it into one function, to handle an error using a different function. Finally, the optional parameters specify we will allow a position which is cached and is as old as 10 minutes but with a timeout of 0 if there is no cached version it will fail.

Inside the successCallback function we can then do something such as:

var latitude, longitude;

function successCallback (position)
{
   latitude = position.coords.latitude;
   longitude = position.coords.longitude;
}

Once we have the longitude and latitude we can then send it to PHP using an Ajax call or a form submission. To get these details the user will first be alerted that the page is requesting them and will then send them if the user permisses it to. The following details can be obtained using the same method:

  • longitude,
  • latitude,
  • altitude,
  • accuracy,
  • altitudeAccuracy,
  • heading,
  • speed.

Other than the obvious use of seeing what services are around you from these values I have thought of another possible use. If you allow your user to set home and work locations you could also give them the option to lock down access to sensitive parts of their user account on sites so that they can only be accessed from those co-ordinates. Of course there are problems such as "what if the user has moved and no longer access to the co-ordinates" or "what if the user can't get GPS co-ordinates on a system" but these could be solved as well. A solution I'd propose for these cases would be to allow unlocking of the restrictions by allowing them to submit a request which sends an "unlock" URL to the user's email account.

One thing I should point out is that the specification is still in draft, and where the W3C specify them being in the format of position.coords.latitude, Geode is currently accessing it as position.latitude meaning further checks for namespacing may be required in your code.

your comments - Post a comment

blog comments powered by Disqus