« To The Editors of WIRED- Where's the Love? | Main | The Mashup UI »

Javascript-based location for browsers using Google Gears

This article will give an introduction to the Google Gears Geolocation API. This API is a Javascript interface for geolocation, and uses Gears to intermediate with location providers (google is the default one). HTTP and JSON are used for communication.

I believe that if you want to location enable the web, you have to location enable Javascript. This is happening through Google Gears, Geode (for Firefox), and ultimately a W3C spec on how to do this for the web browser. I like Gears since it works with the browser, but also any other application that knows the basic protocol Gears uses.

You specify in Javascript the provider(s) to use other that the default on. You form a JSON request and include what you know about how your are connected to the internet. Cell tower info, nearby wifi hotspots, the country you are in (and maybe cool things like Wimax info in the future). This helps the provider get a fix on your position. The request is sent via HTTP POST to all providers. The responses are examined by Gears, and the one with the best reported accuracy is returned as a Position object. All Position objects report latitude and longitude at a minimum. As you may have already guess, they also report accuracy.

Here is some sample information.

provider (default)
http://www.google.com/loc/json

valid POST request to the above provider URL (these are the mandatory request parameters)
{"version": "1.0.1","host": "maps.google.com"}

valid response from provider
{"location":{"latitude":38.95937,"longitude":-77.35455,"horizontal_accuracy":43 000.0}}

News Flash-
Since I posted this, Google changed the API (but didn't update their documents- shame on you!). 'accuracy' is now a required field, although it doesn't appear to be used at all based on my testing. horizontal_accuracy and vertical_accuracy fields no longer exist. How do you know what Gears is doing and what it really expects? The best test is to use Wireshark (formerly Ethereal- http://www.wireshark.org/ ) and look at the actual JSON response from the google default provider. You can also see what Gears is sending to the google default provider. Stick with what they are doing, if not what the docs always say and you will save yourself some frustration.


For more details on the API, see http://code.google.com/apis/gears/api_geolocation.html


What I find most interesting is that you can create your own location provider. Your provider could use GPS, Wimax location services, or a combination of providers. You just have to follow the API for providers located at http://code.google.com/p/gears/wiki/GeolocationAPI

Here's the full example code for how to use your own provider, except for the provider itself. You can just dummy that up with a web page, though. This is taken from http://code.google.com/apis/gears/api_geolocation.html and modified to show how to use a different provider(s). And yes, you really can create a provider that runs at localhost and Gears can talk to it and hand off the location information to the web browser. Try that with AJAX (don't bother- it won't work)!

enclose this code in script tags and call the init() function when the page loads. You must also have a tag to load the gears_init.js file from google.

var geo = google.gears.factory.create('beta.geolocation');

function updatePosition(p) {
alert('Current lat/lon is: ' + p.latitude + ',' + p.longitude );
}

function handleError(positionError) {
alert('Attempt to get location failed: ' + positionError.message);
}

function init()
{
//since this is an array, multiple providers can be used. Gears returns the most accurate location results.
//for example, you could have: var providers = ["http://localhost:8080/json.jsp", "http://www.google.com/loc/json", "http://localhost/mygpsinterface"];

var providers = ["http://localhost:8080/json.jsp"];
var options = ;
geo.getCurrentPosition(updatePosition, handleError, options);
}

TrackBack

TrackBack URL for this entry:
http://www.erichizdepski.com/blog-mt1/mt-tb.fcgi/55


Hosting by Yahoo!

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)