Tuesday, March 16, 2010

Geolocation using the Browser in the Android OS (Part 2)

Yesterday I wrote about my initial explorations with the web browser available as part of the Android operating system. In that post I was using the W3C Geolocation API which is supported in the browser in the more recent releases of the operating system. Older releases do not support this option and so an alternative has to be used.

The alternative comes in the form of the Google Gears for Android library. Using this library it is possible to determine the geolocation of an older Android device. Of course “older” is a relative term as it applies to devices such as the HTC Hero, which I own, and has been available for just over 6 months.

The first change needed to use the Google Gears library is to include the following script tag in the header of the page:

The second is to expand upon the JavaScript in yesterdays post so that it looks like this:

$(document).ready(function() { // Try W3C Geolocation (Preferred) if(navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { // success $('#map_canvas').empty(); $('#map_canvas').append('

Latitude: ' + position.coords.latitude + '
Longitude: ' + position.coords.longitude + '

'); }, function(position_error) { // failure $('#map_canvas').empty(); $('#map_canvas').append('

An error occured while determining your location. Details are:
' + position_error.message + '

'); }, { // options enableHighAccuracy: true }); } else { // W3C Geolocation method isn't available // try Google Gears if (google.gears) { // google gears is available so use it var geo = google.gears.factory.create('beta.geolocation'); geo.getCurrentPosition(function(position) { // success $('#map_canvas').empty(); $('#map_canvas').append('

Latitude: ' + position.latitude + '
Longitude: ' + position.longitude + '

'); }, function(position_error) { // hack to check if we're running in the emulator if(navigator.userAgent.match(/sdk/i)) { // running in the emulator so ignore error and fake coordinates $('#map_canvas').empty(); $('#map_canvas').append('

Running in the emulator

'); } else { // error $('#map_canvas').empty(); $('#map_canvas').append('

An error occured while determining your location. Details are:
' + position_error.message + '

'); } }, { // options enableHighAccuracy: true, gearsLocationProviderUrls: null }); } else { // not available $('#map_canvas').empty(); $('#map_canvas').append('

The W3C Geolocation API isn\'t availble.

The Google Gears API isn\'t availble.

'); } } });

The new code in this version starts on line 20 where a check is performed to ensure that Google Gears is available. Next an instance of the beta.geolocation class is instantiated. The getCurrentPosition method is called using a familiar pattern. The first function will be used if geolocation information can be retrieved, the second function will be called if an error occurs and the last parameter is an options object.

There are a few important things to note:

  1. The gearsLocationProviderUrls is set to null in an attempt to get the Android emulator to use the GPS coordinates supplied to it using the technique I outlined yesterday
  2. The error function uses a simple hack to detect if it is being called by the browser in an emulator and if it is we can fake the coordinates that we want to use

For me geolocation in the emulator is a bit of a “hit and miss” affair and I’m having to make changes in the code to take this into account. Hopefully when we have the appropriate devices available I’ll not have to keep doing this. The emulator is really useful but the geolocation capabilities can be frustrating at times.

It’s also important to note that this code works on Android devices, and my limited testing shows it works on iPhone devices as well, other devices are not supported at this time. Due primarily to time constraints. I intend incorporating a feedback mechanism into this part of the mobile website so that users can alert us to other devices as required.

The planned direction for development from this point is to clean up the code a little bit and start incorporating it with the latest version of the Google Maps API which will introduce a new series of challenges that will need further exploration. These will be the topic of future posts.

[Via http://techxplorer.com]

No comments:

Post a Comment