Sunday, April 17, 2011

 

How HTML5 geolocation works in Firefox, Chrome, and Internet Explorer

HTML5 introduces (will introduce?, will have introduced once it's approved?) the Geolocation API, which enables a web page to ask your browser to ask you if the page can be told where on earth you are.  For example, when composing a tweet, Twitter includes an "Add Your Location" link that uses this new API.

Of course, your web browser doesn't just know where you are; it has to ask something else.  On a device like your mobile phone which includes a GPS receiver, the operating system can tell it.  However, on a notebook without GPS, your browser's not so lucky.  Enter the might power of Google.  In this circumstance both browsers fire off a request to http://google.com/lat/json.  If your computer has a WiFi device turned on at the time, both browsers send along the name, hardware ID, and signal strength of every WiFi base point that your little lappy can see.  When I was sitting at work, the data sent looked like this:

{
    "version": "1.1.0",
    "request_address": true,
    "wifi_towers": [{
        "mac_address": "xx-xx-xx-xx-xx-01",
        "ssid": "XXXXXXXXXXXX",
        "signal_strength": -81
    }, {
        "mac_address": "xx-xx-xx-xx-xx-00",
        "ssid": "XXXXXXXX",
        "signal_strength": -83
    }, {
        "mac_address": "xx-xx-xx-xx-xx-02",
        "ssid": "XXXXXXXXXXX",
        "signal_strength": -83
    }, {
        "mac_address": "xx-xx-xx-xx-xx-30",
        "ssid": "XXXXXXXX",
        "signal_strength": -62
    }, {
        "mac_address": "xx-xx-xx-xx-xx-62",
        "ssid": "XXXXXXXXXXX",
        "signal_strength": -82
    }, {
        "mac_address": "xx-xx-xx-xx-xx-a0",
        "ssid": "XXXXXXXX",
        "signal_strength": -75
    }, {
        "mac_address": "xx-xx-xx-xx-xx-a1",
        "ssid": "XXXXXXXXXXXX",
        "signal_strength": -74
    }, {
        "mac_address": "xx-xx-xx-xx-xx-61",
        "ssid": "XXXXXXXXXXXX",
        "signal_strength": -81
    }, {
        "mac_address": "xx-xx-xx-xx-xx-a2",
        "ssid": "XXXXXXXXXXX",
        "signal_strength": -75
    }, {
        "mac_address": "xx-xx-xx-xx-xx-32",
        "ssid": "XXXXXXXXXXX",
        "signal_strength": -57
    }, {
        "mac_address": "xx-xx-xx-xx-xx-31",
        "ssid": "XXXXXXXXXXXX",
        "signal_strength": -65
    }, {
        "mac_address": "xx-xx-xx-xx-xx-58",
        "ssid": "XXXXXXXX",
        "signal_strength": -76
    }, {
        "mac_address": "xx-xx-xx-xx-xx-60",
        "ssid": "XXXXXXXX",
        "signal_strength": -75
    }]
}
Google then ships back coordinates for where they think you are based on that WiFi data. The response also includes city, state, street address, and an estimate of accuracy.

When I was at work, their guess of accuracy was spooky good. Not only did the Big G put me in the right building, but they also correctly discerned that I was on the south side of that (not terribly large) building.

I also gave it a go from home. There the accuracy was a little less impressive, but still pretty good. Google's guess was off by about 250 meters, but that's probably because I live in a relatively newly built up part of a subdivision. Google put me on the side of the neighborhood that's been around for a few more years.

If your WiFi adapter is turned off, you're not in range of any networks, or your PC doesn't have WiFi, then the request to google.com/lat/json sends an empty set for the WiFi data and Google appears to just use your IP address to determine where you are, returning a city-accurate location. The returned accuracy was reported as 22 km when I tried it.

Internet Explorer 9 uses a similar trick; when asked for geolocation, it sends a request to https://inference.location.live.net/inferenceservice/v21/Pox/GetLocationUsingFingerprint containing WiFi data. IE's request doesn't include network name, interestingly. Microsoft's API is much uglier, relying on XML written in that pedantic way that gives XML a bad name, filled with disgustingly repetitive xmlns junk. In my very small sample size, Microsoft's data was much lower quality. From home, my location was misjudged by about 5 km with a reported uncertainty of 16 km. At work, Microsoft just gave up and said it didn't know where I was. I wonder if MS just fell back on geo-IP locating for the location it provided when I was at home because when I removed all of the WiFi data from the request and resubmitted it, the returned location was identical.

Comments:
how did you get the xml?
 
I got the XML for IE's request by sniffing the requests on the wire. Fiddler2 is my preferred tool for that in Windows.
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?