3

I'm developing an application to track my phone at regular intervals (not only the first time) with navigator.geolocation.getCurrentPosition() running in an HTML5 page running in a webkit webview running in an android 2.1 application.

I already read responses to the following questions:

Using navigator.geolocation.getCurrentPosition in WebView on Android 2.0+ (PhoneGap related)
Android: Using html5 to determine geolocation in webview with javascript api
Geolocation cannot be load on webview
Android WebView using GeolocationPermissions

I managed to get the position through the javascript method navigator.geolocation.getCurrentPosition(), even if with a very bad accuracy (1421 meters).

But the real problem is: if I call the getCurrentPosition() method more than once, it returns me always the same position and always with the same bad accuracy (1421 meters). It seems that one of these 2 cases occurs:

  1. The device does not get the position from the GPS sensor (fine accuracy), but it gets it via AGPS (from the 3g cell) causing the same position and accuracy being returned. The GPS sensor is obviously on, and I've set the right privileges in the manifest file:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_GPS" />
    <uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
  2. The device got the first position correctly after calling "getCurrentPosition", but the next time this method is called, the position is returned from some "GPS cache" and the position is not really got from the GPS sensor.

Has anyone successfully gotten the position updated at regular interval through the getCurrentPosition() HTML5 Javascript method?

Community
  • 1
  • 1
giowild
  • 479
  • 5
  • 17

1 Answers1

4

I've found that phones (at least with iPhones) usually need three checks to get an accurate reading, almost as if they are triangulating the position. The problem, as you have discovered, is that each new call to getCurrentPosition() seems to start "blind", so it doesn't get any more accurate (results vary when you're running this function on a desktop/laptop).

The solution is geolocation.watchPosition(). That function uses previous calls to improve accuracy. After about five seconds, it's as accurate as it's going to get (if you're not moving), so you stop checking. Also, it will stop checking any time it thinks it won't get any more accurate. Use window.setTimeout() to control the interval.

Demo: http://jsfiddle.net/ThinkingStiff/yn3Bq/

HTML:

<div id="result"></div>

Script:

var latitude, longitude, accuracy;

function setGeolocation() {
    var geolocation = window.navigator.geolocation.watchPosition( 
        function ( position ) {
            latitude = position.coords.latitude;
            longitude = position.coords.longitude;
            accuracy = position.coords.accuracy;
            document.getElementById( 'result' ).innerHTML += 
                  'lat: ' + latitude + ', '
                + 'lng: ' + longitude + ', '
                + 'accuracy: ' + accuracy + '<br />';
        }, function () { 
            /*error*/ 
        }, {
            maximumAge: 250, 
            enableHighAccuracy: true
        } 
    );
    window.setTimeout( function () {
            window.navigator.geolocation.clearWatch( geolocation ) 
        }, 
        5000 //stop checking after 5 seconds
    );
};

setGeolocation();

window.setTimeout( function () {
        setGeolocation();
    }, 
    30000 //check every 30 seconds
);

Output:

lat: 35.5830119, lng: -124.4871223, accuracy: 40
lat: 35.5829974, lng: -124.4871525, accuracy: 30

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
  • Thanks for your response and time. I tried this in a webview and seems to work, but I will try it more accurately now :) I'll report it back here! – giowild Dec 19 '11 at 08:40
  • Ok, it definitely works! watchPosition is THE ANSWER! :) Thanks very much thinking – giowild Dec 19 '11 at 13:38