arun
arun

Reputation: 3677

GPS is not getting after disabling and enabling again in android with phonegap

I'm working with a GPS related application in phonegap and cordova.js. My app is working fine in iOS. In android whenever I turn off the gps and enable it again, app will not recognise the GPS services. I need to close and reopen the app again or use location.reload(true) to refersh the app. Why it is like that. I'm using the below code segments.

refrshWatchPoss = setInterval(function () {
            navigator.geolocation.getCurrentPosition(onWatchPositionSuccess, onWatchPositionError, options);
        }, 5000);

function onWatchPositionError(err) {
    window.plugins.toast.showLongBottom('App is waiting for location service! ', function (a) {
    }, function (b) {
    })
    if (watchpositionErrorCount >= 9) {        
        messageAlert('Location service is unavailable. If it is unavailable after restarting then go to settings and reset your location services');        
        $.mobile.loading('show');
        navigator.splashscreen.show();
        setTimeout(location.reload(true), 2000);
    }
    watchpositionErrorCount++;
}

Upvotes: 0

Views: 69

Answers (2)

DaveAlden
DaveAlden

Reputation: 30356

I would suggest using watchPosition rather than getCurrentLocation on an interval, since watchPosition will call the success function each time the operating system retrieves a location update from the GPS hardware, rather than at a specific interval.

I've found that on Android, by clearing and re-adding the position watcher, this works around the issue when location services are turned off then on again. So, using your code above, something like this:

var positionWatchId;

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

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


function onWatchPositionError(err) {
    window.plugins.toast.showLongBottom('App is waiting for location service! ', function (a) {
    }, function (b) {
    })
    if (watchpositionErrorCount >= 9) {        
        clearWatch();
        addWatch();
    }
    watchpositionErrorCount++;
}

addWatch();

Upvotes: 1

user3255670
user3255670

Reputation:

@nu6A, GPS service varies from phone to phone and provider to provider. Unlike iPhone there are many manufactures, and manufactures of phone vary which chips they use - even in the same model!

If you are encountering this problem, you will need to work around it. You should also look for as much documentation as you can.

Best of Luck

Google: android how does gps work

Upvotes: 1

Related Questions