0
votes

I am developing hybrid mobile application using cordova for android.I want to get the current location from the device for the i am using the geolocation plugin in cordova.But when after calling the

navigator.geolocation.getCurrentPosition(onSuccess, onError);

it is always going to onError callback and showing timeout error.if i am giving timeout option

If i am not giving the timout option..the timeout error is not showing and the output also not coming please give some solution for this.

2
Are you testing it in a real device or in eclipse emulator? - TlmaK0

2 Answers

0
votes

First, double check GPS device is on when the app is started ,-)

Second, Try to add option enableHighAccuracy false to your getCurrentPosition call, and or test with cache, option maximumAge. If you have no answer with a long timeout, maybe GPS device is slow. It could take up to 25 seconds to obtain GPS information. So be patient ,-)

 navigator.geolocation.getCurrentPosition(
            function(positionGPS){console.log(positionGPS);},
            function(){console.log('no gps');},
            {enableHighAccuracy:false,maximumAge:Infinity, timeout:60000}
           );  
0
votes

I try multiple strategies until found a result, if not I do a pull every minute. More or less like this:

function calculatePosition(timeout, maxAge, accuracy, step){
  navigator.geolocation.getCurrentPosition(
      function(position){
        console.log("success");
      },
      function(error){
        nextStep(step + 1);
      }, 
      {enableHighAccuracy: accuracy,
      timeout: timeout,
      maximumAge: maxAge}
    );
}

function nextStep(step){
  switch(step){
    case 0:
      calculatePosition(1000, 604800000, false, step);
      break;
    case 1:
      calculatePosition(1000, 604800000, true, step);
      break;
    case 2:
      calculatePosition(5000, 4000, false, step);
      break;
    case 3:
      calculatePosition(5000, 4000, true, step);
      break;
    case 5:
      calculatePosition(50000, 60000, true, step);
      break;
    case 6:
      calculatePosition(50000, 60000, false, step - 2);
      break;      
  }
}

nextStep(0);