What it is
Typically included as part of the “HTML5 technologies,” the W3C’s Geolocation API Specification (a proposed recommendation as of 10 May 2012) defines an “API that provides scripted access to geographical location information associated with the hosting device.” In other words, it allows one to figure out where a browser (desktop, laptop, mobile) is using JavaScript. It also happens to be an incredibly simple API to use.
While the API is consistent across devices, how a given device determines its position is not. There are a number of different ways to determine (such as GPS) or infer (IP address, RFID, Wi-Fi and Bluetooth MAC addresses, etc.) location. Not all methods are available to all devices, and some of them are (far) more accurate than others.
The geolocation API is implemented as a new property on the navigator object: navigator.geolocation.
Source: Geolocation API Specification. W3C Proposed Recommendation 10 May 2012
Where it can be used
- Chrome 5.0+
- Firefox 3.5+
- Internet Explorer 9.0+
- Opera 10.6+
- Safari 5.0+
- Android 2.0+
- iPhone 3.0+
- Opera Mobile 10.1+
Sources: When can I use…, HTML5 Doctor
How to detect it
return !!navigator.geolocation;
Source: Dive Into HTML5
Known issues and concerns
Privacy concerns
Since sharing the physical location of one’s device has obvious privacy concerns, the geolocation API is explicitly opt-in. All user agents must first obtain permission to share via a user interface (similar, but not consistent across all devices).
Sources: Geolocation API Specification, Dive Into HTML5
IE9 accuracy
Internet Explorer 9 appears to have some accuracy issues when determining latitude and longitude. For more information, see this Microsoft TechNet forum thread.
Source: When can I use…
Polyfills
While there are several geolocation polyfills available, HTML5 Please recommends:
- geo-location-javascript
- Primarily for mobile websites/widget applications. Includes support for Blackberry OS, Nokia, webOS, browsers with Google Gears support and more.
More information
While any of the Source links included above can be used for more information, the following are what I consider the best concise sources to learn more about using the geolocation API:
- Dive Into HTML5. No. 6. You are Here (and so is Everybody Else)
- HTML5 Doctor. Finding your position with Geolocation
Last updated on 12 July 2012.
Please add any updates or corrections in the comments section below. Thanks!