Search code examples
androidcordovaionic-frameworkgeolocation

Can't get location with cordova on android 5.1


I'm facing an issue with location exclusively on android 5.1 and lower (everything is working fine on 6). I’m using cordova-plugin-geolocation 2.4.2 (latest version). This is how we are fetching the location.

$ionicPlatform.ready(function () {
    var posOptions = {enableHighAccuracy: true};
    $log.debug('LocationService.getLocation: getting live location');
    navigator.geolocation.getCurrentPosition(function (position) {
        $log.debug('LocationService, position:', position);
    }, function(err) {
        $log.debug('LocationService.getLocation: Failed to fetch location.', err);
    }, posOptions);
});

With this configuration the function getCurrentPosition never returns a location. It is just running endless.

I tried with various posOptions combinations but nothing works:

var posOptions = {enableHighAccuracy: false}; // endless
var posOptions = {enableHighAccuracy: false, timeout: 30000}; // triggers a PositionError (code 3 => timeout)
var posOptions = {enableHighAccuracy: true, timeout: 30000}; // triggers a PositionError (code 3 => timeout)
var posOptions = {enableHighAccuracy: true, timeout: 30000, maxAge: 0}; // triggers a PositionError (code 3 => timeout)

There is my ionic info

Cordova CLI: 6.4.0
Ionic CLI Version: 2.1.18
Ionic App Lib Version: 2.1.7
ios-deploy version: 1.9.0
ios-sim version: 5.0.13
OS: macOS Sierra

Permission are in the AndroidManifest.xml

<uses-permission android:name="android.permission.NETWORK_ACCESS" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-feature android:name="android.hardware.location.gps" />

Again it is working fine on Android 6, the problem only occurs the lower versions. I already looked for answers but none was working.


Solution

  • I had to bind my function to the deviceready event