Mobile HTML5

Teaching developers how to build great mobile web apps with HTML5.

Curated by Ian Sefferman and supported by AppStoreHQ. With AppStoreHQ, you can list your mobile web app and accept payments easily.

Oct 9

Android 2.2 Browser Geolocation gotcha

I’ve been using fortysquires as my exclusive Foursquare app for many months now. I used it on iPhone and, for the last three or so months, on Android (Droid X). Everything was working great until my Droid X was updated to Android 2.2 (FroYo) and the GPS code stopped working.

It wasn’t just on fortysquires, either. It was on many sites that rely on geolocation. In fact, one of the only sites that did work was (for a while, even Google’s mobile homepage didn’t show my location, but it seems they’ve recently fixed it).

In any case, searching for the root cause was mostly fruitless, so through a long amount of trial-and-error, I found the bug. It’s very simple, but not obvious.

It’s all about enableHighAccuracy

Previously, the relevant portion of my code looked something like this (simplified):

navigator.geolocation.getCurrentPosition(handleSuccess, errorCallback);

However, in order to make Android 2.2 work (and, perhaps some updates of Android 2.1), you must add the enableHighAccuracy option:

navigator.geolocation.getCurrentPosition(handleSuccess, errorCallback, { enableHighAccuracy: true });

In all honesty, I’m not sure what prompted the change by Android developers, but whatever it is, this is the solution. For a 29 character change, this took me about 10 hours of experimentation and debugging to figure out. 

In case you’re wondering, according to the W3C spec, enableHighAccuracy means:

The enableHighAccuracy attribute provides a hint that the application would like to receive the best possible results. This may result in slower response times or increased power consumption. The user might also deny this capability, or the device might not be able to provide more accurate results than if the flag wasn’t specified. The intended purpose of this attribute is to allow applications to inform the implementation that they do not require high accuracy geolocation fixes and, therefore, the implementation can avoid using geolocation providers that consume a significant amount of power (e.g. GPS). This is especially useful for applications running on battery-powered devices, such as mobile phones.

I hope this helps you as you look for the solution to this bug!

  1. termpaperss reblogged this from mobilehtml5
  2. web-development-performance-blog reblogged this from mobilehtml5
  3. mobilehtml5 posted this