Software Archive
Read-only legacy content
17061 Discussions

Simulating GPS Timeout/Delay doesn't seem to work as expected.

Nick_F_2
New Contributor III
1,193 Views

I have an app that has to capture GPS location data and also test from a cellular or Wifi Connection.

In my code I have a timeout of 5 seconds with HighAccuracy set to True.

I am trying to determine the result if no GPS signal is present by increasing the GPS delay to more than 5 seconds in the emulator, but I cannot get it to fail not to return a GPS location. If I simulate a timeout then that just throws an error that GPS is not enabled.

function getLocation(){
window.plugins.spinnerDialog.show("Please Wait....","Collecting Data.", true);
var options = { 
  enableHighAccuracy: true,
  timeout: 5000,
  maximumAge: 0 };
if (navigator.geolocation){
var ConnState = localStorage.getItem("connection");
    if (ConnState == "yes") {
navigator.geolocation.getCurrentPosition(saveReading, onError,options);
    }
    else{
 navigator.geolocation.getCurrentPosition(saveLocal, onError,options);       
    }
}
    else
    {
if (ConnState == "yes") {
navigator.geolocation.getCurrentPosition(saveReading, onError,options);
    }
    else{
 navigator.geolocation.getCurrentPosition(saveLocal, onError,options);       
    }
    }
}

The saveReading and saveLocal functions both use the following to determine location but what do I get back if there is no GPS location data

function saveLocal(position) {
    var lat = position.coords.latitude;
    var lng = position.coords.longitude;

The code first checks for a cellular or Wifi connection then a GPS connection and determines whether to save directly or locally to upload later.

0 Kudos
3 Replies
John_H_Intel2
Employee
1,193 Views

Have you tried this in App Preview or by building your app and installing it on device?

GPS is one of the plugins that are difficult emulate in the emulator.

0 Kudos
Nick_F_2
New Contributor III
1,193 Views

Hi John

Yes tried it on iOS and Android. The Android device I have has the ability to turn of GPS but still I get a GPS result.

0 Kudos
PaulF_IntelCorp
Employee
1,193 Views

You should experiment using the "hello cordova" sample app. It will help you get a good feeling as to what to expect and what not to expect from GPS on real devices. Five seconds is far too short a time to wait for fine GPS signals, it can take as much as a minute on some devices to get a real GPS lock. You should first get an approximate location using the coarse request and then switch to fine for more accurate data.

When using the hello cordova sample, the "watch" button will use the settings corresponding to the most recent coarse or fine button. Or you can modify the code to further adjust. Note that the timeout is set to 30 seconds in that app.

0 Kudos
Reply