Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android geolocation using phonegap code: 3 error

I'm trying to use geolocation using PhoneGap API doc but getting below error message,

Alert code:3 message: Timeout expired

My code is,

    <script>

// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);

var watchID = null;

// Cordova is ready
//
function onDeviceReady() {
    // Throw an error if no update is received every 30 seconds
    var options = { timeout: 10000 };
    watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
}

// onSuccess Geolocation
//
function onSuccess(position) {
    var element = document.getElementById('geolocation');
    element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +
                    'Longitude: ' + position.coords.longitude + '<br />' +
                    '<hr />' + element.innerHTML;
}

// onError Callback receives a PositionError object
//
function onError(error) {
    alert('code: ' + error.code + '\n' +
      'message: ' + error.message + '\n');
}

Please advice me what I am missing.

like image 318
user2331670 Avatar asked Nov 27 '13 10:11

user2331670


3 Answers

I believe this is a relatively common problem especially with Android devices. Some of the popular solution I have come across include

  1. Do not specify the error callBack: For some really odd reason this works, your only problem will arise if GPS isn't enabled. I wouldn't advise this in a production app though.

  2. Specify enableHighAccuracy: true in your options. This seems to work but not on its own, see #3 below

  3. Specify maximumAge in options: I use this one and set it very high to maybe 1hour. What it does is that even if there is no GPS currently, cordova will give you the last known coordinates captured within the last hour, so this really depends on how accurate you want your coordinates to be with respect to time.

I usually use a combination of #2 and #3 and this works reasonably for Phonegap 3.x.

As an side, you could include an option to start an Intent via a plugin for the device's settings in your error callback so that the user can enable their GPS if disabled (Like google maps on the phone does when you use it for navigation).

I hope this helps.

like image 154
Obi Avatar answered Nov 08 '22 05:11

Obi


Assuming you are getting this problem in an Android emulator:

  1. Add a timeout and set enableHighAccuracy:

    navigator.geolocation.getCurrentPosition(onSuccess, onError, {timeout: 10000, enableHighAccuracy: true});
    

    In certain emulators you need to set enableHighAccuracy to false, so try that if still doesn't work.

  2. In Android, the emulator don’t read GPS values, so we need to send them via command line. We need to start a telnet session in the port that the emulator is running (you can check the port in the emulator window title, the number at the beginning, in my case 5554):

    telnet localhost 5554
    

And then run the command

    geo fix -122.4 37.78

If you close the app you need to re-send the geolocation, so if it doesn’t work, just run the geo fix command just after opening the app, before the timeout event fires.

If you are using a real Android device, make sure it has GPS enabled, and that you installed the geolocation plugin (run phonegap local plugin add org.apache.cordova.geolocation)

like image 21
Jesús Carrera Avatar answered Nov 08 '22 05:11

Jesús Carrera


I solved this problem for an HTC M8 running the latest Android (physical device) by not specifying maximumAge and timeout.

navigator.geolocation.getCurrentPosition(onSuccess, onError, {
  // maximumAge: 100,
  // timeout: 2000,
  enableHighAccuracy: true
});

On iPhone 5 it worked fine, but the Android only returned errors. Now both work without problems. I'll note that I had even tried 1 hour maximumAge and high timeouts, and that didn't work. This is the only solution that worked for all scenarios.

like image 2
Caleb Pitman Avatar answered Nov 08 '22 06:11

Caleb Pitman