Search This Blog

Saturday, 10 March 2012

First Stab at Geolocation

Like the blog post title says, I've just had a first go at using a browser's Geolocation abilities. I'd imagined it would be quite involved, but nothing could be further from the truth. There are only three functions in the Geolocation API, and they're almost trivial to use.

This is the nub of it:

    navigator.geolocation.getCurrentPosition(function (position) {
        // Do something with the position data.
    });

I particularly like the way they put it all into the 'navigator' object.

'position' contains your co-ordinates and an accuracy. When I tried it in Chrome on my PC the stated accuracy was 25km, but the co-ordinates did look very much like York's. Of course, this begs the question of how my browser knows where my PC lives, given that it doesn't have GPS in it.