Cordova / Phonegap-geolocation error

I am writing a Cordova / Phonegap application and I am using the Geolocation plugin ... this is my code ...

var onSuccess = function(position) {
    longitude = position.coords.longitude;
    latitude = position.coords.latitude;

    console.log("Latitude: "+position.coords.latitude);
    console.log("Longitude: "+position.coords.longitude);
};

function onError(error) {
    logService.debug("Code: "+error.code);
    logService.debug("Message: "+error.message);
};

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

      

Now I test it in the browser and when I don't get permission I get error code 1 (PositionError.PERMISSION_DENIED), when I do this permission it works fine in the browser ... Now the question was born ... When I test this on device and the GPS is off, I don't get error code 1 (PositionError.PERMISSION_DENIED), but I always get a timeout ... this way I can't tell the difference ... I set the timeout to 150000, but I always get code 3 (PositionError .TIMEOUT) ... Why? How to use it in the strict sense?

+3


source to share


1 answer


I found that the geolation error handling is platform related. Since the installation enableHighAccuracy: true

forces your app to ask the operating system to recover the position using the hardware GPS, the effect of disabling GPS on an Android device depends on the Android version: either the OS can never get a high-precision position, so a TIMEOUT error occurs (PERMISSION_DENIED will not be received on Android). or a low fidelity location will be selected and transmitted using Wifi / cell triangulation instead.

I would suggest using watchPosition()

instead getCurrentPosition()

to get the location; getCurrentPosition () makes a single request for the position of the device at this point in time, so the position timeout can occur before the GPS hardware on the device has a chance to get the position fix, whereas when used watchPosition()

you can tweak which will call the success function every time when the OS receives a location update from the GPS hardware. If you only need one spot, clear the observer after getting enough accuracy. If GPS is disabled on an Android device while adding an observer, it will keep returning a TIMEOUT error; my workaround for this is to clean up and re-add the observer after a series of successive errors.



So try something along these lines:

var MAX_POSITION_ERRORS_BEFORE_RESET = 3,
MIN_ACCURACY_IN_METRES = 20,
positionWatchId = null, 
watchpositionErrorCount = 0,
options = {
    maximumAge: 60000, 
    timeout: 15000, 
    enableHighAccuracy: true
};

function addWatch(){
    positionWatchId = navigator.geolocation.watchPosition(onWatchPositionSuccess, onWatchPositionError, options);
}

function clearWatch(){
    navigator.geolocation.clearWatch(positionWatchId);
}

function onWatchPositionSuccess(position) {
    watchpositionErrorCount = 0;

    // Reject if accuracy is not sufficient
    if(position.coords.accuracy > MIN_ACCURACY_IN_METRES){
      return;        
    }

    // If only single position is required, clear watcher
    clearWatch();

    // Do something with position
    var lat = position.coords.latitude,   
    lon = position.coords.longitude;
}


function onWatchPositionError(err) {
    watchpositionErrorCount++;
    if (watchpositionErrorCount >= MAX_POSITION_ERRORS_BEFORE_RESET) {        
        clearWatch();
        addWatch();
        watchpositionErrorCount = 0;
    }

}
addWatch();

      

0


source







All Articles