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.

24 comments

Post a comment
  1. Pingback from nitot's status on Tuesday, 09-Jun-09 12:11:23 UTC - Identi.ca on June 9th, 2009 at 5:11 am:

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

    Reply

  2. Pingback from The Praized Blog » Blog Archive » Geolocation in Firefox 3.5: It’s Coming! on June 9th, 2009 at 6:10 am:

    [...] 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 [...]

    Reply

  3. Pingback from Firefox 3.5 does geolocation! | Squio.blog on June 9th, 2009 at 8:29 am:

    [...] 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 [...]

    Reply

  4. Pingback from 颠覆网络35天 ─ Firefox 3.5中的地理定位 < MJiA on June 9th, 2009 at 11:39 pm:

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

    Reply

  5. Pingback from 35 días para el Firefox 3.5 | ceronegativo on June 10th, 2009 at 5:35 pm:

    [...] geolocation in Firefox 3.5 [...]

    Reply

  6. Pingback from Ajaxian » Animating SVG with Canvas and Burst on June 11th, 2009 at 5:56 am:

    [...] geolocation in Firefox 3.5 [...]

    Reply

  7. Pingback from Animating SVG with Canvas and Burst | Programming Blog on June 11th, 2009 at 4:18 pm:

    [...] geolocation in Firefox 3.5 [...]

    Reply

  8. Pingback from geolocation with open street maps at hacks.mozilla.org on June 12th, 2009 at 7:52 pm:

    [...] 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 [...]

    Reply

  9. Pingback from geolocation in Firefox 3.5 – web technologist on June 21st, 2009 at 1:40 pm:

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

    Reply

  10. Pingback from 7 Reasons Why We are Excited about Firefox 3.5 « Building Feedly on June 30th, 2009 at 12:13 pm:

    [...] 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 [...]

    Reply

  11. Pingback from synchronous XHR requests in Firefox 3.5 at hacks.mozilla.org on July 1st, 2009 at 10:06 am:

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

    Reply

  12. Polski Sklep wrote on July 3rd, 2009 at 4:40 am:

    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.

    Reply

  13. Ray Watkins wrote on July 6th, 2009 at 4:17 pm:

    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?

    Reply

  14. Pingback from 谋智社区 » Blog Archives » 颠覆网络35天 ─ Firefox 3.5中的地理定位 on July 6th, 2009 at 7:57 pm:

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

    Reply

  15. Pingback from Earle Martin (hex) 's status on Saturday, 11-Jul-09 19:54:20 UTC - Identi.ca on July 11th, 2009 at 12:54 pm:

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

    Reply

  16. Pingback from Ponte al día con HTML5. | pon css en tu vida! on July 13th, 2009 at 3:23 am:

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

    Reply

  17. Tim wrote on August 8th, 2009 at 5:25 am:

    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.

    Reply

  18. Sascha Hendel wrote on August 17th, 2009 at 9:23 am:

    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

    Reply

  19. Pingback from » Where are the Firefox geolocation add-ons? My Green Life on August 20th, 2009 at 9:26 am:

    [...] 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 [...]

    Reply

  20. Pingback from Mozilla Hacks: Geolokalizacja w Firefoksie 3.5 « marcoos.techblog on August 20th, 2009 at 2:34 pm:

    [...] 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 [...]

    Reply

  21. Pingback from Cross-platform mobile application development and payment | Twinapex Blog on September 30th, 2009 at 2:55 am:

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

    Reply

  22. Pingback from Geo: Soon to be Legit « Web Strategy Consulting on November 12th, 2009 at 1:13 am:

    [...] 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 [...]

    Reply

  23. z.Yleo77 wrote on November 14th, 2009 at 1:24 am:

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

    Reply

  24. Pingback from Use geolocation to get your current address from Google maps on December 2nd, 2009 at 6:51 am:

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

    Reply

Add your comment