Geolocation getCurrentPosition continuously fails if first try fails on android


#1

In my ionic app i wanna use geolocation with the plugin https://github.com/apache/cordova-plugin-geolocation.

If i start the app (HTC One M7 with Android 4.4.4) with Location enabled, everything works fine (getCurrentPosition and watchPosition).

If Location is disabled and you try to ask for geolocation with watchPosition or getCurrentPosition, you get an error as expected.
But after that, when you enable Location on the device, you get the same error again if you try to check geolocation.

You can check this example [https://github.com/xelita/angular-cordova-plugins-sample].

Don’t know, if it’s the same on iOS.

Thanks for help!


#2

I think you should use timeout with navigator , Some time its take some time to initialize GPS position. I also faced some type of problem , used timeout then its worked fine.

    google.maps.event.addDomListener(window, 'load', function() {
        navigator.geolocation.getCurrentPosition(setCurrentPosition, showGPSError, {
            enableHighAccuracy: true,
            timeout: 10000
        });
    });

#3

Thanks for replying.

Even if i set a timeout to infinite, i can’t get any geolocation, if location is not enabled before.
Set enableHighAccuracy to true is not an option for me.

I tried https://github.com/dpa99c/cordova-diagnostic-plugin/ to check isLocationEnabled periodically, but it doesn’t deliver the same result on Android and iOS for same settings. It needs GPS to be enabled (not WiFi or 3G) on android for returning true.
I created an issue, maybe this can be fixed. Then everything would be fine.


#4

You should enable GPS to get current location


#5

You can’t tell the user to definitely switch on GPS.
Maybe he is used to use low accuracy (3G / WiFi). Or maybe the device has no GPS sensor built in. Or you are in a situation, where no GPS is available but 3G or WiFi.
And it should be ok, if the user enables geolocation during runtime of app and don’t has to do it before starting the app.

It should behave as common apps like maps.

Thanks anyway.

I think, https://github.com/dpa99c/cordova-diagnostic-plugin/ is the solution/best workaround for me: check isLocationEnabled() periodically and make the first call of getCurrentPosition() or watchPosition() if geolocation is enabled.