W3C Geolocation API
The W3C Geolocation API is an effort by the W3C to standardize an interface to retrieve the geographical location information for a client-side device[1]. It defines a set of objects, ECMAScript standard compliant, that executing in the client application give the client's device location through the consulting of location information servers, which are transparent for the API. The most common sources of location information are IP address, Wi-Fi and Bluetooth MAC address, RFID, Wi-Fi connection location, or device GPS and GSM/CDMA cell IDs. The location is returned with a given accuracy depending on the best location information source available.
Deployment in Web Browsers
Web pages can use the Geolocation API in two ways: directly, if the web browser implements it (as is the case with Mozilla Firefox 3.5 and that latest beta of Google Chrome); or through Google Gears[2]. In the latter case, the client browser must have the Gears plugin installed, and the web page must initialize Gears with the following HTML:
<script type="text/javascript" src="gears_init.js" />
The Geolocation API is ideally suited to web applications for mobile devices such as PDAs and smartphones. However, there is not yet widespread support on such platforms due to the wide variety of devices and mobile browsers (which usually lack a plugin architecture). On the desktop, the W3C Geolocation API works in Firefox since version 3.5, iPhone and Android (firmware 2.0 +) and in development builds of Opera 10.5.[3] and Google Chrome[4].
Google Gears provides geolocation support for Internet Explorer 7.0+ (as Gears plugin), and Google Chrome (which implements Gears natively). It also supports geolocation on mobile devices as a plugin for the Android Browser (pre version 2.0) and Opera Mobile for Windows Mobile.
Basic Use
The most important Object to declare in your code is the Geolocation one, which implements the main methods to retrieve and populate the object Position, which will be used to show user's device position.
Example
Simple Javascript code that checks if the browser has the Geolocation API implemented or it has the Google Gears to create the Geolocation object and then uses it to get the current position of the device.
var gl = null;
function displayPosition(position) {
p = document.getElementById("p");
p.innerHTML = "<table border='1'><tr><th>Timestamp</th><td>"+ position.timestamp +
"<tr><th>Latitude (WGS84)</th><td>" + position.coords.latitude + " deg</td></tr>" +
"<tr><th>Longitude (WGS84)</th><td>" + position.coords.longitude + " deg</td></tr></table>";
}
function displayError(positionError) {
alert("error")
}
try {
if(typeof(navigator.geolocation) == 'undefined'){
gl = google.gears.factory.create('beta.geolocation');
} else {
gl = navigator.geolocation;
}
}catch(e){}
if (gl) {
gl.getCurrentPosition(displayPosition, displayError);
} else {
alert("I'm sorry, but geolocation services are not supported by your browser.");
}
References
- ^ "W3C Geolocation API Specification", Editor: Andrei Popescu from Google Inc. , 22 December 2008. Retrieved 07 July 2009
- ^ Geolocation API - Gears API - Google Code (9 July 2009)
- ^ http://my.opera.com/desktopteam/blog/2010/02/05/skin-fixes-unite-and-then-some
- ^ http://googlechromereleases.blogspot.com/2010/03/dev-channel-update.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+GoogleChromeReleases+%28Google+Chrome+Releases%29
External links
- Firefox 3.5 Geolocation Features for developing location applications.
- Tutorial and example of Geolocation API with Google Gears.
- Add a network location provider to Gears.