geolocation in Firefox 3.5

This post is from Doug Turner, one of the engineers who is behind the Geolocation support in Firefox 3.5.

Location is all around us. As of this writing, I am in a coffee shop in Toronto, Canada. If I type google into the url bar, it takes me to www.google.ca, the Canadian version of Google, based on my IP address. And when I want to find the closest movie theater to where I am located, I typically just type in my postal code. That information is often stored with the site so that it’s easier to find the movie theater next time. In these two situations, having the web application automatically figure out where I am is much more convenient. In fact, I have no idea what the postal code is for Toronto. I know how to find it, but that is a lot of work to simply tell a web application where I am.

Firefox 3.5 includes a simple JavaScript API that allows you to quickly geo-enable your web application. It allows users to optionally share their location with websites without having to type in a postal code. What follows is a general overview of how to use geolocation in Firefox 3.5, how it works, and examines some of the precautions you should take when using geolocation.

The Basics

Getting the user’s location is very easy:

function showPosition(position) {
    alert(position.coords.latitude + “ “ +
    position.coords.longitude);
}

navigator.geolocation.getCurrentPosition(showPosition);

The call to getCurrentPosition gets the users current location and puts it into an alert dialog. Location is expressed in terms of longitude and latitude. Yes, it’s that simple.

When you ask for that information the user will see a notification bar like this:

Their options are to allow or not, and to remember or not.

Handling Errors

It is important to handle two error cases in your code:

First, the user can deny or not respond to the request for location information. The API allows you to set an optional error callback. If the user explicitly cancels the request your error callback will be called with an error code. In the case where the user doesn’t respond, no callback will be fired. To handle that case you should include a timeout parameter to the getCurrentPosition call and you will get an error callback when the timer expires.

navigator.geolocation.getCurrentPosition(successCallback,
                                         errorCallback,
                                         {timeout:30000});

With this code your errorCallback function will be called if the user cancels. It will also be called if 30 seconds pass and the user hasn’t responded.

Second, the accuracy of the user’s location can vary quite a bit over time. This might happen for a few reasons:

  • Different methods for determining a person’s location have different levels of accuracy.
  • The user might choose not to share his or her exact location with you.
  • Many GPS devices have limited accuracy depending on the view of the sky. Over time if your view of the sky gets worse, so can the accuracy.
  • Many GPS devices can take several minutes to go from a very rough location to a very specific location, even with a good view of the sky.

These cases can and will happen, and supporting changes in accuracy is important to provide a good user experience.

If you want to monitor the location as it changes, you can use the watchPosition callback API:

navigator.geolocation.watchPosition(showPosition);

showPosition will be called every time there is a position change.

Note that you can also watch for changes in position by calling getCurrentPosition on a regular basis. But for power savings and performance reasons we suggest that you use watchPosition when you can. Callback APIs generally save power and are only called when required. This will make the browser more responsive, especially on mobile devices.

For more information, please take a look at the API draft specification which has other examples which may be useful.

Under the Hood

There are a few common ways to get location information. The most common are local WiFi networks, IP address information, and attached GPS devices. In Firefox 3.5 we use local WiFi networks and IP address information to try and guess your location.

There are a few companies that drive cars around listening for WiFi access points spots and recording the access point’s signal strength at a specific point on the planet. They then throw all of this collected data into a big database. Then they have algorithms that allow you to ask the question “If I see these access points, where am I?”. This is Firefox 3.5’s primary way of figuring out your location.

But not everyone has a WiFi card. And not every place has been scanned for WiFi. In that case Firefox will use your local IP address to try to figure out your location using a reverse database lookup that roughly maps your IP address to a location. IP derived locations often have much lower accuracy than a WiFi derived location. As an example, here in Toronto, a location from WiFi is accurate to within 150 meters. The same location using just an IP address is about 25 km.

Privacy

Protecting a user’s privacy is very important to Mozilla – it’s part of our core mission. Within the realm of data that people collect online, location can be particularly sensitive. In fact, the EU considers location information personally identifiable information (PII) and must be handled accordingly (Directive 95/46/EC). We believe that users should have strict control over when their data is shared with web sites. This is why Firefox asks before sharing location information with a web site, allows users to easily “forget” all of the places that the user has shared their location with, and surfaces sharing settings in Page Info.

Firefox does what it can to protect our users’ privacy but in addition the W3C Geolocation working group has proposed these privacy considerations for web site developers and operators:

  • Recipients must only request location information when necessary.
  • Recipients must only use the location information for the task for which it was provided to them.
  • Recipients must dispose of location information once that task is completed, unless expressly permitted to retain it by the user.
  • Recipients must also take measures to protect this information against unauthorized access.
  • If location information is stored, users should be allowed to update and delete this information.
  • The recipient of location information must not retransmit the location information without the user’s consent. Care should be taken when retransmitting and use of HTTPS is encouraged.
  • Recipients must clearly and conspicuously disclose the fact that they are collecting location data, the purpose for the collection, how long the data is retained, how the data is secured, how the data is shared if it is shared, how users may access, update and delete the data, and any other choices that users have with respect to the data. This disclosure must include an explanation of any exceptions to the guidelines listed above.

Obviously these are voluntary suggestions, but we hope that it forms a basis for good web site behavior that users will help enforce.

Caveats

We have implemented the first public draft of the Geolocation specification from the W3C . Some minor things may change, but we will encourage the working group to maintain backwards compatibly.

The only issue that we know about that may effect you is the possible renaming of enableHighAccuracy to another name such as useLowPower. Firefox 3.5 includes the enableHighAccuracy call for compatibility reasons, although it doesn’t do anything at the moment. If the call is renamed, we are very likely to include both versions for compatibility reasons.

Conclusion

Firefox 3.5 represents the first step in support for Geolocation and a large number of other standards that are starting to make their way out of the various working groups. We know that people will love this feature for mapping applications, photo sites and sites like twitter and facebook. What is most interesting to us is knowing that people will find new uses for this that we haven’t even thought of. The web is changing and location information plays a huge role in that. And we’re happy to be a part of it.

About Christopher Blizzard

Making the web better, one release at a time.

More articles by Christopher Blizzard…


29 comments

  1. […] article about geolocation in Firefox 3.5: http://hacks.mozilla.org/2009/06/geolocation/ […]

    June 9th, 2009 at 05:11

  2. […] 3.5 is on the verge of being released and it will include geolocation. According to this post, “Firefox 3.5 includes a simple JavaScript API that allows you to quickly geo-enable your web […]

    June 9th, 2009 at 06:10

  3. […] of the engineers who is behind the Geolocation support in Firefox, wrote a nice background story geolocation in Firefox 3.5 (hacks.mozilla.org). A very interesting read, and it turns out that geolocation is not only for […]

    June 9th, 2009 at 08:29

  4. […] 原文地址:geolocation in Firefox 3.5 […]

    June 9th, 2009 at 23:39

  5. […] geolocation in Firefox 3.5 […]

    June 10th, 2009 at 17:35

  6. […] geolocation in Firefox 3.5 […]

    June 11th, 2009 at 05:56

  7. […] geolocation in Firefox 3.5 […]

    June 11th, 2009 at 16:18

    1. lia hart

      friefox thanks

      April 4th, 2011 at 02:42

  8. […] days ago we had a post from Doug Turner describing how Geolocation works in Firefox 3.5. René-Luc has taken the geolocation functionality in Firefox 3.5 and blended it together with data […]

    June 12th, 2009 at 19:52

  9. […] in this link you get to see how to access the core API […]

    June 21st, 2009 at 13:40

  10. […] Reason #3 – Geo-location. Firefox 3.5 includes support for geo-location. This means that user can grant applications permission to know about their location. In the context of feedly, this means that we will soon be able to work with some partners and offer you a “Near you” section in your cover and digest. More relevant information + Less input/configuration = Good. Learn more about Geo Location support […]

    June 30th, 2009 at 12:13

  11. […] post is from Doug Turner who has previous written about Geolocation. Doug works on Mozilla’s mobile […]

    July 1st, 2009 at 10:06

  12. Polski Sklep

    An alternative service – which enable geolokalization on most web browsers – offers loki.com. On my website I choose to support Mozilla geolocation for Mozilla browsers and Loki for others.

    July 3rd, 2009 at 04:40

  13. Ray Watkins

    I have yet to see anywhere an example or discussion regarding how the Geolocation API integrates with an attached GPS. In my iPhone it is handled by the OS but I can’t find any documentation.

    Does anyone have any idea how to stitch together a GPS receiver on a com port or usb port and the geolocation api in firefox 3.5?

    July 6th, 2009 at 16:17

  14. […] 原文地址:geolocation in Firefox 3.5 […]

    July 6th, 2009 at 19:57

  15. […] @evan This would be so awesome for Laconi.ca – find local users: http://hacks.mozilla.org/2009/06/geolocation/ […]

    July 11th, 2009 at 12:54

  16. […] Geolocalización en Firefox 3.5 | Mozilla Hacks […]

    July 13th, 2009 at 03:23

  17. Tim

    I find it surprising that Firefox’s Geolocation feature does not look for and use an attached GPS device to determine the browser location. Surely this should be the first thing it should look for, and only fall back to other methods if this fails? The WiFi method is very clever, but GPS is likely to be more reliable and more accurate if available.

    August 8th, 2009 at 05:25

  18. Sascha Hendel

    The firefox (wifi/ip-based) geolocation support is great, but I would suggest to include a settings menu into firefox to manually set your location!
    If You have a desktop computer, the geolocation will change not very often ;) and it would be easy (and much more exact) to set your location manually once instead of using ip or wifi detection.

    Sascha

    August 17th, 2009 at 09:23

  19. […] more about the geolocation feature at the Mozilla Hacks site, and based on the comments there, I’m not the only one wondering where the GPS […]

    August 20th, 2009 at 09:26

  20. […] serii tłumaczeń artykułów z bloga Mozilla Hacks, przedstawiam dzisiaj tłumaczenie artykułu Geolocation in Firefox 3.5, którego autorem jest Doug Turner, jeden z twórców obsługi geolokalizacji w Firefoksie […]

    August 20th, 2009 at 14:34

  21. […] The first taste of this is Mozilla’s Fennec mobile browser which has locationing support. […]

    September 30th, 2009 at 02:55

  22. […] new nature of the API draft spec. Opera has implemented the spec in builds for several months, and it will be implemented in the release of Firefox 3.5. More recently, it’s become evident that Mobile Safari in the new iPhone 3.0 software […]

    November 12th, 2009 at 01:13

  23. z.Yleo77

    a useful function in the future … but it needs supports by most of browses.. ff is a great example

    November 14th, 2009 at 01:24

  24. […] hacks.mozilla.org: geolocation in Firefox 3.5 […]

    December 2nd, 2009 at 06:51

  25. […] Geolocation […]

    April 29th, 2010 at 17:12

  26. Vitold S

    Ok. I am look around and look Opera with Geolocation API and I am try using it. And I am retrive position but Geolocation API is lie….

    I am have GPS in my Windows XP Home and FireFox can’t handle it. I am use CityGide, anoter navigation system and all right but FireFox is lie.

    Can I develop plugin and get the plugin for work with standart COM port NMEA GPS navigator???

    Who can help me with this? Developer of Firefox?

    September 20th, 2010 at 01:41

  27. bob

    how can i turn this invasion of privacy off?
    i just came from google where i noticed it showed my town name when i did a search on it’s new side bar rubbush.
    it tells you at the page where it links to that they do this all the time automatically unless you have the toolbar. i am not an apple sheep nor google sheep. i do not collect software because it has a trendy name.
    this digusts me the behaviour of assumption and i thought firefox may know better.
    turns out it’s you guys that enable it for them.
    so where is my built in off switch to say f-off. when i want a local info search i will do one.
    regards
    unamused person

    November 15th, 2010 at 10:18

  28. […] hacks.mozilla.org: geolocation in Firefox 3.5 […]

    May 10th, 2011 at 02:40

Comments are closed for this article.