Sunday, December 19, 2010

HTML5: The Geolocation API is scary (good)

I read about the HTML5 Geolocation API in the Pro HTML5 Programming book a while ago, and decided to play with it on this lazy Sunday.

Using the Geolocation API to make a one-shot position request is very straight-forward. Get a reference to the navigator.geolocation object and call the getCurrentPosition() method, passing in at least a PositionCallback. In this example I'm also passing in a PositionErrorCallback. In the PositionCallback you can examine the properties of the position object. Here I am only using the latitude and longitude properties.

function showLocation(){                        
    if (navigator.geolocation){
        navigator.geolocation.getCurrentPosition(onSuccess, onError);
    } else {
        var content = document.getElementById("content");        
        content.innerHTML = "Geolocation is not supported by your browser!";
    }                
}
 
function onSuccess(position){
    var content = document.getElementById("content");        
    content.innerHTML = position.coords.latitude + ", " + position.coords.longitude;
}
 
function onError(error){
    var content = document.getElementById("content");        
    content.innerHTML = "Something went wrong..";
}

That's really easy, right? You can find the complete source and demo here.

The scary part

The privacy part isn't that scary, because the specs state that browsers must acquire permissions through a user interface. The scary part is, that it really works and is very accurate even though I'm on a desktop with no known hotspots nearby! After getting the results, I pasted them in Google Maps and they were only a few meters off. Why is it that while most geolocation services have been failing over the years, it suddenly works this good?

There is some documentation out there, but this documentation is very simplistic and doesn't touch the internals.

The Pro HTML5 Programming book lists which sources can be used.
A device can use any of the following sources:
  • IP address
  • Coordinate triangulation:
    • Global Positioning System (GPS)
    • Wi-Fi with MAC addresses from RFID, Wi-Fi and Bluetooth
    • GSM or CDMA cell phone IDs
  • User defined
FireFox lets the Google Location Services assist them.
If you consent, Firefox gathers information about nearby wireless access points and your computer’s IP address. Then Firefox sends this information to the default geolocation service provider, Google Location Services, to get an estimate of your location. That location estimate is then shared with the requesting website.

Feel free to share more good resources!

What is the outcome on your machine?

Check out the demo, paste the result in Google Maps and please let me know how accurate the results are for you.

15 comments:

  1. Mine is pretty accurate. 10-20 meters from where I live. 51.1773336, 4.9861926

    ReplyDelete
  2. Damn, scary indeed.
    quite accurate for me

    ReplyDelete
  3. interesting. I'm working from home today and it locates the address where I used to live one year ago. Provider is Telenet so I assume that they are giving same IP range to customers when they move. Scary indeed. Or do you think about other possibilities.

    ReplyDelete
  4. Didn't work too well for me from work. 16.7 km from my current location. I'll have to try again from home.

    ReplyDelete
  5. Not good at all, showed me in a different city (the country was right though).

    ReplyDelete
  6. Holy shit that thing is 10 meter's accurate.
    How does it work? i thought one could only pinpoint your location to the nearest isp server.

    ReplyDelete
  7. Man... 3 or 4 meters accurate in my case!!!

    ReplyDelete
  8. Around 130km off my actual location.

    ReplyDelete
  9. The topic you discuss here is very informative.Geolocation API makes programming more easy.The example you post here is also helpful and after reading concept of Geolocation API is clear.

    ReplyDelete
  10. Mine pointed right across the street from me, which incidentally, is where the cable guys have their box of cable guts on a pole.

    ReplyDelete
  11. wrong country given, location off by about 300km. what's all the fuss?

    ReplyDelete
  12. off by about two miles, desktop running Vista, Lexington KY

    ReplyDelete
  13. Not really, About 20 km off for me

    ReplyDelete
  14. Great article. Useful information. Thanks for sharing this information.
    http://www.cavinitsolutions.com/

    ReplyDelete