Geolocation Redux and a JS Library
After the excellent feedback I got on my last geolocation blog post, I’ve updated the proposed geolocation API. More importantly, I’ve also created a pure Javascript library which you can use to start playing around with geolocation before we actually get it into the browser. This library provides exactly the same API as would be provided by the browser. Although it can’t offer GPS-quality data, it does provide a decent city-level geolocation based on your IP. You can get the library here, and there’s also a no-frills demo.
For quick hacking you can just include the following in your head tag.
<script src="http://azarask.in/projects/geolocation/MockGeolocation.js"><script>
The best way to get a feel for the API is to see some examples:
var g = new navigator.GeolocationRequest();
g.request( function(geolocation){
alert( geolocation.longitude + ", " + geolocation.latitude );
})
This is the same example as we saw last time; it alerts the user’s current location. What’s new is that I’ve spec’ed out the object passed to the callback (I’ve borrowed some of the great work done for the GoogleGears Location API):
interface Geolocation {
readonly double latitude; // in degrees
readonly double longitude; // in degrees
readonly double altitude; // in meters, or null if no data.
readonly double accuracy; // in meters
readonly double altitudeAccuracy; // in meters, or null if no data
int timestamp; // time of the location read, in seconds since
// the epoch
}
Everything here should be self-explanatory. As a commenter mentioned last time, it’s important to know when the last location reading occurred. Without it, for example, it would be impossible to reliably calculate the current speed of the browsing device. (Knowing such data gives developers all sorts of fun—we could have an onDrunkDriving DOM event for when the browser detects your moving at 60MPH and weaving!).
Let’s move on to something a bit more complicated.
var g = new navigator.GeolocationRequest();
g.request({
error: function(e){ alert( e.message ); },
success: function(location){
alert( location.altitude + "+/-" + location.altitudeAccuracy );
},
desiredAccuracy: g.defaultAccuracy
})
Here’s the new stuff:
error takes a callback whose argument is an object. The only attribute the object must have is message, a human readable explanation of the error. The error object may have other attributes, as dictated by the particulars of the location-giving device.
desiredAccuracy is a string which can take one of three values: “exact”, “neighbourhood”, and “city”. In order, “exact” means to return a geolocation as precisely as possible, “neighbourhood” means to return a geolocation good to 1km, and “city” means to return a geolocation good to 10km. It’s unnecessary to include a “country” option because there are more accurate ways of knowing what country a user is browsing from than with a highly fuzzed lat/long (e.g., IP-based geolocation).
defaultAccuracy is a read only property of the GeolocationRequest object that contains the user’s globally-set preference for yielding location to content providers. If the user has opted to give all websites city-level access to their location, then a website requesting city-level information won’t cause the browser to prompt for permission. In the above example, the request is asking for the highest accuracy location that won’t prompt the user for permission. defaultAccuracy is null if the user has opted out of providing geolocation information.
That about wraps it up for the API. Full interface documentation is available.
Why not on window.navigator?
It’s tempting to attach the GeolocationRequest object to window, but it makes more semantic sense to place it on the navigator object (and, unfortunately, not just for the pun). The navigator object contains meta information about the browsing agent—browser version, os, language, etc—and geolocation information is just that. Looking at it from the other side, the window object should contain information that is tied to the current tab/window. Geolocation is independent of what tab/window you are looking at (unless, of course, you have an extremely large screen), and so should not be attached directly to window.
Geocoding API
I’m somewhat on the fence about whether to include geocoding as part of the fundamental GeolocationRequest API. The information is useful to web developers, and it is a boon to not require an extra Ajax call to perform geocoding. However, it’s going beyond the scope of the fundamental language of geolocation. To quote Arun Ranganathan, our standards hero here at Mozilla:
That aside, we don’t really have to choose. By designing the API right, we can upgrade to include geocoding at a later date, without breaking any code. Let’s see it in action:
var g = new navigator.GeolocationRequest();
g.request({
error: function(e){ /* Handle error. */ ); },
success: function(location){ alert( location.address.city ); },
desiredAccuracy: "neighborhood",
requestAddress: true,
addressLanguage: "en-US"
})
You can get the library for the address-enabled API here. The address object interface is:
interface Address {
// Any of the below can be null, if not known.
readonly string streetNumber; // street number
readonly string street; // street address
readonly string premises; // premises, e.g. building name
readonly string city; // city name
readonly string county; // county name
readonly string region; // region, e.g. a state in the US
readonly string country; // country
readonly string countryCode; // country code (ISO 3166-1)
readonly string postalCode; // postal code
}
The full specification is available, if you are interested in such sundry documentation.
Interface
Based on a number of good suggestions from the last post, this is how I’m thinking the security UI should look:

RT @aza Geolocation Redux and a JS Library | Follow @aza on Twitter | All blog posts
Tags: Firefox, geolocation
Blair McBride
Looks good. I think it would more important to see this in Fennec (the up-coming mobile version of Firefox) than the desktop Firefox. But hopefully having APIs like this will make us see more GPS receivers in laptops – maybe even desktops too.
One question though:
What will webpages see if the user’s isn’t hooked up to a GPS receiver? ie, will navigator.GeolocationRequest be undefined? Or will it return null? Or will it return a full data structure with all values as null?
I think that part of the API is just as important, as most people won’t have any GPS data. But not having data is far different from just having a browser that doesn’t support geolocation.
comparer forfait
I would have to say maybe invest in a faster internet. If it is slow and you try to do two things at once then that is what usually happens.
Aza Raskin
@Blair: The hope is to have one web available everywhere. I.e., that the same JS APIs can be used on your mobile device and your computer. As for the not-hooked-up-to-a-GPS case, all we have to do is to have a number of fallbacks. The worst fallback can be a ping to Mozilla to do a geoip lookup. A better fallback can be to use wifi-based geolocation technology.
Ryan Freebern
I’m not sure of a good solution for this, but when I glanced at the text labels on that security UI and thought “1 mile… 1 kilometer… oh wait, the first one must have been 1 *meter*…” and when I asked a couple U.S. friends, they confirmed that’s what they thought initially too.
Ted Mielczarek
Sounds like you need some i18n for your units of distance. :)
Aza Raskin
@Ryan/Ted: Ah, dear. Yes. Consider those units to be correctly localized. We can even have them in nautical miles and furlongs ;)
Ian Bicking
I have a hard time reading the person size for accuracy, as its the inverse of the region size. Maybe a little of both? That is, make the circles a little larger as the area increases, and the person a little smaller.
The radius thing seems vague to me. If it gives the center of the radius in each case, then of course there’s no difference. So what information is really given? If it’s a city name, or a zip code, or something like that… can you actually show what is sent? If it’s an area, and you happen to be right on the border, that could be nice to know — for example, I’ve had that problem when looking for something like a restaurant based on zip code. Or, what if the information is inaccurate? How can you check that? What if you want the information to be inaccurate? Like, you are going to be somewhere else later, and you want to get information about that area?
Some other little details… what if you don’t have exact information available? My laptop is not capable of knowing exactly where I am. Can I select my location on a map? From a list of locations I’ve been at before? Can the browser learn things, like when my externally-visible IP address is a certain value, it means I’m at the office.
As a user, I would probably be happier if there was a consistent UI for all the location-related things. That is, if the API you give is really hooked up to something fairly sophisticated that gives all these options.
Daiane
cmrheyborb619 on August 24, 2011 can I have the link please?
Aza Raskin
@Ian: Thanks for the graphic feedback. I’ve updated it also modulate the circle size. In terms of the radius thing, all it is doing is forcing an increase in the error. That is, if you select “neighborhood”, then Ars Technica with get back a lat/long that is good to within +/- 1km of your actual location. So Ars Technica might think you are .7km northwest of your actual location. At least for the first round of geolocation in the browser, you won’t get automatic access to geocoding so that there will never be the border problem you described.
The basic idea with the location api is to abstract, as you say, all the difficulties of actually getting your location. So on the backend, it will try GPS, wifi-based location, IP-based location, ESP, whatever it can to get your location as best it can. But as a user, you’ll never see that.
Ian
” I believe that location is such vital context, Powerbooks should come with GPS receivers pre-installed, with an easy software API. Developers would then write software to take advantage of it, and other computer makers would follow suit. Someday, a computer without GPS might seem as silly as a computer without a clock.” Bret Viktor
Nice to see that you are following him and extending some of the great ideas he’s proposed.
Gregor J. Rothfuss
Finally!
Are you aware of Doug Turners work?
http://dougt.wordpress.com/2006/08/08/support-geolocation-in-the-browser/
Another one is http://blog.liip.ch/archive/2007/05/20/the-whereami-firefox-extension.html
Aza Raskin
@Gregor: I work with Doug, so yes. I called him out in particular in my first blog post about geolocation.
Doug Schepers
I like the proximity granularity, but I would also like to have a duration UI selector. I might like to allow a webapp to get my location over a certain duration, but no more than that. I suppose I could navigate away from a webapp or shut down a widget, but it’s something to consider.
Jeffrey Morgan
A more humane way to label proximity granularity would be to label the scale with the geolocation information itself. Instead of using
exact location
neighbourhood
city
nothing
as the scale, the browser would label the scale with each part of the address of the user’s location. For example, if the user was located in Office number 123 at Google’s headquarters, the scale would be:
Office 123
1600 Amphitheatre Parkway
Mountain View
California
USA
Users would then be able to say that they don’t mind an app knowing that they are in Mountain View, but telling the app that they are in Amphitheatre Parkway would be too much information.
The advantage of an address-based proximity scale is that it doesn’t use country- and language-specific terms, such as neighbourhood and city, but does use the address format familiar to users in each country.
Aza Raskin
@Jeff: That’s a phenomenal idea! I like it a lot. The biggest problem, though, is that geocoding data might not be available. For example, with a GPS you won’t know the user’s non-lat/long location information without a round-trip to a geocoding server. And even then, you might not be returned useful results. I think the solution I gave might be a good fallback mechanism… but any more thoughts about what happens when you only have access to lat/long?
Jeffrey Morgan
If you only have the lat/long then you can still form a rough address. One can work out the city from a lat/long which would then give the country. If the lat/long is in the woods, a desert, or some other place not near a city, then the lat/long still gives the country, which the browser can then present as a simple “Nothing” or “USA” choice (if the user in the USA).
The browser can only present the location information that it has available. The user interface for the scale must be flexible, which is why I believe an address-based scale is more suitable. We are all used to dealing with incomplete addresses, or addresses that are less detailed than others. Using my Google headquarters address example, all of the following would make humane scales:
USA
Nothing
California
USA
Nothing
Mountain View
California
USA
Nothing
Amphitheatre Parkway
Mountain View
California
USA
Nothing
Another important interface issue is the level of detail from the human perspective. A pair of lat/long co-ordinates accurate to a metre is technically very accurate, but the name of the street the user is in is more informative and meaningful to people, even though it is technically less accurate. Users might also consider a street name more detailed than their numerical lat/long position because names are more meaningful to us than numbers, but you’d need to do some user testing on that.
eldemia
Занимаюсь дизайном и хочу попросить автора http://www.azarask.in отправить шаьлончик на мой мыил) Готов заплатить…
Zayıflama Lida Fx15 Ve Biber Hapı Zlfvbh
Looks good. I think it would more important to see this in Fennec (the up-coming mobile version of Firefox) than the desktop Firefox. But hopefully having APIs like this will make us see more GPS receivers in laptops – maybe even desktops too.
porno
Looks good. I think it would more important to see this in Fennec (the up-coming mobile version of Firefox) than the desktop Firefox. But hopefully having APIs like this will make us see more GPS receivers in laptops – maybe even desktops too.
Daniela
I don’t see ayhnting different with Safari 4 on my Mac, or on the browser on my Palm Pre (which is based on a slightly older version of WebKit than Safari 4). What am I supposed to see?
Sex
Looks good. I think it would more important to see this in Fennec (the up-coming mobile version of Firefox) than the desktop Firefox. But hopefully having APIs like this will make us see more GPS receivers in laptops – maybe even desktops too.
gucci belts
It retains mens belts cheap gucci belts cheap louis vuitton belts for men the new cheap desiger belts shape Stretch Jeans Slender gucci belts on sale September louis vuitton belts cheap Have you ever thought it was a method as one million U.S. dollars without sacrificing comfort;
Melati
Re: nuembr one, I totally agree! EARN, the CA-based nonprofit I work for, helps low-income workers save money to invest in assets, including small businesses. I’ve been thinking of how cool it would be for an app to link CA residents up with our Savers’ businesses and reward them for being regular patrons!
nmjeraxp
lDZ573 nghtansveodm
gucci belts
C Beijing China mens belts NBA star center Yao Ming cheap gucci belts GHD can cry without pain, to the Court in the eyes of basketball cheap louis vuitton belts for men fans, but some irreducible on the possibility of cheap desiger belts the first born of the soul would have gucci belts on sale U.S. passports. louis vuitton belts cheap Yao and his wife returned to the United States MBT, where he plays for the Houston Rockets before the birth of her first child, which should eventually took place between May and July.
odszkodowanie
Very interesting publication
sikis
I think that’s a pretty neat idea. My current watch is a Timex that mimics a chronograph digitally but has both an analog and digital face: I can hide the digital part of it by pressing a button and leave the clean lines of a analog watch (which I prefer). Since I enjoy the benefits of digital for cycling and training and such, it’s nice to have that option.
porno
yirmi üç yorum yapılmıştı sitene benle birlikte yirmi dörk oldu
دردشه بنات
difference. So what information is really given? If it’s a city name, or a zip code, or something like that… can you actually show what is sent? If it’s an area, and you happen to be right on the border, that could be nice to know — for example, I’ve had that problem when looking for something like a restaurant based on zip code. Or, what if the information is inaccurate? How can yo
basur
Nice to see that you are following him and extending some of the great ideas he’s proposed.
py3r3str
hey
is it still working? I think I get not as precision data as i was getting few days ago.
شات كتابي
thnks
goooooooooooood
min:)
Amit
This will work only in forefox ??
pellet mill manufacturer
Users would then be able to say that they don’t mind an app knowing that they are in Mountain View, but telling the app that they are in Amphitheatre Parkway would be too much information.
شات صوتي
thnks
goooooooooooood
min:)ااا
دردشة صوتية
I like such topics
icons downloads
Easier on turns!
P.S. Please review icons
isabel marant sneakers
Hello There. I found your blog using msn. This is a very well written article.
I will be sure to bookmark it and return to read
more of your useful information. Thanks for the post.
I’ll certainly return.