Despite the idea of "being in Cyberspace" and the power of the Internet to connect us to people all over the world regardless of our location, we often use a browser to find out about people and services close to us. These might be queries such as finding the opening times of a local store, the time of a train or local expertise. Each time we do this it is often necessary to tell the website where we are, typcially by providing a post code, but what if you don't know the postcode? Fortunately browsers and becoming much more clever, and some can even work out where you are.
Since the arrival of Firefox 3.5, Mozilla have made available to developers functionality to find, with their consent, the location of the user. I've been meaning to look into this for a while, so I thought I'd have a look at the developer documentation to see how this works. Writing the code to use this functionity seemed straightforward enough, and I made a small code sample to demonstrate geolocation in action. Mozilla's implementation follows the first draft of the W3C's Geolocation API Specification, some other browsers follow this too so the good news if you want to wrote geolocation enabled web pages that work across different platforms.
If you use a device with built a built in GPS receiver it is often possible to pass the location from this to the geolocation functionality, but what if you are just using a desktop or laptop? Here comes the surprise, amazingly the location functionality may still work. Firefox can use Google Location Services to work out where you are based on your WiFi access point and IP address. When I tried this out I was amazed at how accurate it was, using a laptop with no GPS built in, the location was returned was actually in the next street! It also is possible to write providers for Firefox too to tell it a user's location.
There is of course a privacy implication here, and the data returned by the location service could be quite sensitive and also could be covered by data protection laws. If you want to find a location the user must explicitely agree to share it with your site. When you page loads and you ask for this information, a dialogue box will appear asking this permission. They might say no, so that is an error condition that has to be handled, the location might also be unavailable too. So below is my attempt at creating a self contained demonstration of this technology in action. You can save it to an html file on your machine or even put it on a web server to experiment. It doesn't do anything too exciting, just finds the longitude and latitude of the user and then uses these to construct a URL for Google Maps. This is shown to the user by replacing the contents of some tags on the page with updated information.
Latitude: 0.00 Longitude: 0.00
On line 39 of the code you will see a line that says "if (navigator.geolocation)", this tests whether the browser being used has the geolocation functionality. If not an error message is shown. Adoption of this functionality is far from universal at the moment, I found this code sample works on Firefox 3.5, Firefox Mobile and MicroB on the Nokia N900 and the Apple iPhone. It did not work on IE7, Google Chrome or the G1 Android phone, which was a bit disappointing. If you try this out on other phones it would be great to hear from you.
The magic line to get the position is:
This could be really handy for mobile devices as you could write location aware applications that run server side, potentially saving the effort and expense of writing different applications for each platform. As it works on desktops too it could be used to enhance the experience of using many websites used to find local information. Of course it might take some getting used to, so if I was implementing this on a website I think I'd be careful about making sure users know what is going on and that they are in control of it.
- geolocation in Firefox 3.5: http://hacks.mozilla.org/2009/06/geolocation/
- Using Geolocation: https://developer.mozilla.org/En/Using_geolocation