Jump to content

W3C Geolocation API

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Sonu27 (talk | contribs) at 22:05, 4 May 2010 (→‎Deployment in Web Browsers: source: http://googlechromereleases.blogspot.com/2010/05/beta-channel-update.html). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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