Geolocation getCurrentPosition continuously fails if first try fails on android

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!

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

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.

You should enable GPS to get current location

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.

sir in my case getCurrentPosition are not fire initially now our GPS is On not fire function or not get any error callback but logout then login again same scenrio now function is firenow,tell me why not triigger verry 1st time.i have fire function inside

IonViewDidLoad(){ 
   //code for load map.....
}

All i can say here is the same as i said in my previous post. If you have problems getting geolocation to work initially you should first check if geolocation is available and activated using https://github.com/dpa99c/cordova-diagnostic-plugin/ .

Sir i have already check GPS enable or not using location accracy plugin,but not working 1st time.
Tell me one thing sir its compulsory to call
getCurrentPosition() function inside platform.ready() of a seperate ts file not call from app.com.ts file.
Actually i have trigger getCurrentPosition() Only ionViewDidLoad() not ready function?

It’s safe to call a cordova plugin functions inside platform.ready(), see https://ionicframework.com/docs/v3/api/platform/Platform/#ready :

ready()

Returns a promise when the platform is ready and native functionality can be called

But using ready() is really necessary depends on your code so i can’t tell you in your case.

I don’t know the app initialisation timeline precisely, if platform.ready() callbacks run before ionViewDidLoad() callbacks but you could debug the whole thing by deactivating all your geolocation code and running getCurrentPosition() in a $timeout or manually via DevTools to make sure that the first call of getCurrentPosition() works at all. If it works then you can be sure that the cordova geolocation plugin works and is not ready to be used at the time you tried so far in your code and you need to call it later by using platform.ready() for instance.

sir i have alread called function inside platform.ready function as well as also check GPS enable or not.watchPosition is not fire and i am not get any error callback.i am using cordova 7.0.0 and Android 6.4.tell me what i do ?

I used the capacitor plugin and it works perfectly even along side Cordova