Geolocation in the Browser

Firefox 3.5 makes it super simple to discover the location of a user on your website  You can read more about it from Doug Turner or the official Mozilla page, but today I want to look at how to use the new API.

The following image is a screenshot of geolocation plus Google maps. If you’re running Firefox 3.5, we’ll replace that with a real map showing your current position.

Geolocation services finding me at the Mozilla office.

Geolocation finding me at the Mozilla office.




First, we should check that the browser supports the Geolocation API:

if (!navigator.geolocation) { alert('Get a better browser'); }

Now that we’ve cleared out the riff-raff we can dive in to the new feature:

navigator.geolocation.getCurrentPosition()

I love the simplicity of this API; that’s all you need to know to get the user’s position! To do something useful, we’ll need a function to handle a successful positioning call:

navigator.geolocation.getCurrentPosition(function(position) {
    alert(position.coords.latitude + ", " + position.coords.longitude);
});

Since it takes a few seconds to gather location data and send it up to the geolocation services, we must use an asynchronous callback instead of a return value.

The Position object has two fields: timestamp and coords. position.coords holds all the position and velocity information, with the two most interesting (on my stationary desktop) being latitude and longitude. The motion information will be a lot more fun on mobile devices, especially in conjunction with navigator.geolocation.watchPosition.

This blog goes over the most basic usage of the geolocation API, but I have a slightly more involved example available at http://people.mozilla.com/~jbalogh/geo.html. If you view the source you can check out geolocation, -moz-border-radius, and -moz-box-shadow in action.

NOTE: due to a bug in the current Firefox 3.5b4, the geolocation services may fail on repeated calls. The bug has been fixed, but we’re stuck with it in the current release.

17 responses

  1. Cato wrote on :

    Why did it miss my location by more than a kilometer?

  2. Ray R wrote on :

    How do I turn off geolocation permanently? I do not want any site I visit snooping on me or my whereabouts.

  3. bcrowder wrote on :

    Cato: It’s IP-based if you’re not using a device with a GPS connected to it (such as a smartphone), so you’re being associated with a nearby hub whose location is known.

  4. Ka-Hing Cheung wrote on :

    On my desktop I just get “Couldn’t figure out your position. You can’t count on computers.”

  5. David wrote on :

    This is handy, but I don’t understand why I can’t tell Firefox the exact location of my desktop. For a laptop – ok IP lookup is great, but I have my *exact* address. Why should I ever have to type it in rather than just hitting “Yes, give this site my location”?

  6. steve wrote on :

    To turn off the feature, reveal the hidden preference geo.enabled in about:config and set to false.

  7. Frédéric Wenzel wrote on :

    For more info on the geolocation feature and for answers to privacy concerns (including steps to permanently disable the feature, if you desire so), take a look at http://www.mozilla.com/firefox/geolocation .

  8. RH wrote on :

    This should be disabled by default.
    I am getting increasingly fed up of Google and of Firefox’ association with Google.

  9. Mark Gosdin wrote on :

    As long as the code for this exists in Firefox you can never be certain that you have complete control over who does or does not know your location.

  10. Frédéric Wenzel wrote on :

    RH: I understand your concern. Please note that the feature is indeed opt-in. As long as you do not click on the button to tell a site-owner your location, it will not happen. Firefox will not send geolocation data to anyone unless you want it to.

    Mark: That’s the beauty of Open Source Software. If you want, you can know precisely what Firefox does. Please rest assured that Mozilla absolutely respects your privacy.

    Please also refer to the geolocation page on mozilla.com that I mentioned a few comments earlier.

  11. Matt wrote on :

    I can’t find geo.enabled in my about:config. I’m using FF 3.5 B 4.

  12. Florin Negoita wrote on :

    Geolocation API is **developing** draft of a W3C standard and there are serious discussions about proper approach to the privacy aspect. It is open and transparent for everybody and **any browser vendor** can implement it as they see fit. There is no reason to frown upon Mozilla for taking a lead on this 🙂

    For the vast majority of non-mobile users, the simple IP geolocation is remarkably precise anyway. What the API does is merely exposing that info to location-aware applications.

    Nonetheless, perhaps a checkbox under Privacy to “Enable Geolocation” would alleviate many concerns.

  13. sri wrote on :

    Nice….information.Thanks…

  14. Mr. T wrote on :

    I don’t have to worry, it shows my location as >160kms away 😀

  15. KS wrote on :

    Let’s ask this: “Why would I want others to be able to locate me through a built in Browser App. ?”

    #1. As Florin Negoita Says:

    “What the API does is merely exposing that info to location-aware applications.”

    Ok so #1 tells us that it only works if someone has the technical ability to use the built in App.

    #2. As David says, “I don’t understand why I can’t tell Firefox the exact location of my desktop. For a laptop – ok IP lookup is great, but I have my *exact* address. Why should I ever have to type it in rather than just hitting “Yes, give this site my location”?”

    Ok so #2 tells us that it would work better if we could communicate with websites which used a location box that worked with the browser to automatically type in our location except on an occasional roaming laptop.

    #3. As Mark Gosdin says: “As long as the code for this exists in Firefox you can never be certain that you have complete control over who does or does not know your location.”

    Ok so #3 tells us that an addon (which can be used if preferred or needed) would be more user preferable than a built in location spy that can be conceivably hacked into.

    So, “Why would I want others to be able to locate me through a built in Browser App. ?”

    Truth is, I wouldn’t.

    #1 Reason is based on someone who is for this app.
    It is not convenient, as David said above, it would be more convenient to have an Addon that could simply put the proper address in place.

    #2 is simply privacy

    Yes, Mozilla is Open source and we can know what it is doing, but that does not change that fact that this being built into the browser is a security and privacy risk, which cannot be removed from the software, like an Addon would be capable of. Even if the Addon came standard with the browser, at least those opting out would be able to remove this code from the browser by removing the Addon.

    I don’t feel that this is going to destroy Mozilla’s popularity, but it is a first step in that direction. First you will have a small divide over this issue, next you will have a web-browser developed by and for those concerned, and eventually you will have Mozilla caring less for those they lose because of this, until finally, there will be little to no difference between Mozilla and Google Chrome in terms of privacy.

    So with such little convenience and concern to the majority of privacy conscience users, we must ask, what is the real motive for adding this code? What is its benefit and who does it benefit?

    While this may benefit Mozilla and their partners, there are clearly better and more useful options from the user standpoint.

  16. toupil wrote on :

    j’utilise la géolocalisation de firefox sur mon site web de petites annonces “toupil.fr” afin de localiser les visiteurs et leur proposer uniquement les annonces qui les concernent.

    c’est très pratique … quand cela fonctionne.
    en attendant plus de fiabilité, je propose toujours d’indiquer la localisation à la main.

    j’attends les retours de mes visiteurs pour voir la proportion de géolocalisation ratée de firefox 🙂

  17. Leif Harmsen wrote on :

    Why Google?! Why not simply keep an open IP-to-location table that is independent of Google? As for mapping, you can use openstreetmap – or any map for that matter. I don’t like this heaping on of Google dependant functionality – you can become practically beholden with proprietary shenanigans like that.