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.
Geolocation Demo
Latitude: 0.00 Longitude: 0.00
Re: Where are you? Find out with geolocation in Javascript.
Interesting article, guessing geolocation is also a feature of Firefox 3.6 also?
Re: Where are you? Find out with geolocation in Javascript.
That's right, Firefox 3.6 also introduced support for GPSD on Linux systems.
Re: Where are you? Find out with geolocation in Javascript.
Hello,
I would like to know you are the Liam Green-Hughes from Castletown on the Isle of man !!!
If yes, I am not sure you'll remember me, I was a French teaching assistant from Brittany wher you cam once .