Mulletfingers999 Mulletfingers999 - 2 months ago 15
Javascript Question

Cordova getCurrentPosition() never returns

My cordova version is 6.3.1, and I'm using cordova-plugin-geolocation 2.3.0.

Upon calling

navigator.geolocation.getCurrentPosition(onSuccess, onError)
, neither
onSuccess
or
onError
is ever fired.

I have also tried using the cordova-plugin-locationservices plugin which utilizes Google Play Services for obtaining user location, but to the same result.
I am building for CyanogenMod 13 (Android 6), and am using microG for Google Play Services (Which works in every other app I have come across to date).

Code is below, thank you in advance.

setInterval(function(){
cordova.plugins.locationServices.geolocation.getCurrentPosition(function(pos) { /* cordova-plugin-locationservices */
//navigator.geolocation.getCurrentPosition(function(pos) { /* cordova-plugin-geolocation */
alert('Location determined');
console.log(pos);
}, function(err) {
alert('code: ' + err.code + '\n message: ' + err.message);
});
},3000);

Answer

you have to give an option object with a timeout property, because if you don't, android device won't fire the error callback and so you won't see any error.

setInterval(function(){
       cordova.plugins.locationServices.geolocation.getCurrentPosition(function(pos) { /* cordova-plugin-locationservices */
       //navigator.geolocation.getCurrentPosition(function(pos) { /* cordova-plugin-geolocation */
          alert('Location determined');
          console.log(pos);
        }, function(err) {
          alert('code: ' + err.code + '\n message: ' + err.message);
        }, { timeout: 5000} );
    },3000);

see the doc: https://github.com/apache/cordova-plugin-geolocation

Comments