Search code examples
javascriptandroidcordovageolocationcyanogenmod

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.

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);

Solution

  • 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