getCurrentPosition not working any more after clicking button if GPS is off on the device even if it turned on again

Hi,
I 'm new in ionic world and it’s my first question on this forum :slight_smile:

I have un problem with the use of Geolocation.getCurrentPosition() .
I’m testing my app on an android device.

I use a button to center a Google Maps on the user location.

If I start my app with GPS on, all is ok = when I click the button the Geolocation.getCurrentPosition() is working.

If I start my app with GPS off, if I dont click the button and turn the GPS on and after I click the button, it’s also working.

But if I start my app with GPS off and I click the button, getCurrentPosition is not working any more even if I turned on again the GPS.
Same thing appen if I start the app with GPS on, turn off, click button, tun on, click button => no more getCurrentPosition.

I think I have to test if the GPS is on before firing getCurrentPosition but I dont know how to do that.

Thinks for your help.

Here my code :

Button in the template file:

 <button fab fab-center fab-bottom style="z-index: 999" (click)="geolocMe()">
 <ion-icon name='pin'></ion-icon>
 </button>

My method in the map.js file :

    geolocMe() {
     Geolocation.getCurrentPosition().then((position) => {
       let latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
       this.markerPosi = new google.maps.Marker({
         map: this.map,
         position: latLng
       });
       this.map.panTo(latLng);

     });

   }
 Cordova CLI: 6.1.1
 Ionic Framework Version: 2.0.0-beta.6
 Ionic CLI Version: 2.0.0-beta.25
 Ionic App Lib Version: 2.0.0-beta.15
 OS: Windows 7 SP1
 Node Version: v4.4.3

This sounds like an issue with the geolocation plugin in general :thinking:
This is tough, but I think we might want to create a vanilla cordova example and use the raw cordova plugin to test this out.

Ok thank you for your answer !

Hello I am facing this issue till now. Did this got fixed by ionic team.
Thanks !
Your system information:

Cordova CLI: 6.3.1
Gulp version: CLI version 3.9.1
Gulp local: Local version 3.9.1
Ionic Framework Version: 2.0.0-beta.11
Ionic CLI Version: 2.0.0-beta.37
Ionic App Lib Version: 2.0.0-beta.20
OS:
Node Version: v5.10.0

I have similar problem: my app requires access to GPS. If GPS is enabled when app starts, geolocation works fine. However, if GPS is disabled when the app starts, I turn it on from the app using cordova-plugin-request-location-accuracy. After that geolocation turns to absolutely unreliable: sometimes it works, sometimes it doesn’t. When it doesn’t work, it throws Timeout expired exception, doesn’t matter how big the timeout is.

Any help will be highly appreciated! Thanks!

It seems that I solved the problem by adding

enableHighAccuracy: true

option to getCurrentPosition call:

this.geolocation.getCurrentPosition({ timeout: 30000, enableHighAccuracy: true })

I suspect it somehow related to the fact that the call that triggers GPS enabling also uses high accuracy:

this.locationAccuracy.request(this.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY)

It all looks a little bit like a black magic, but it seems to be working.

2 Likes

Thank you @skydive !!! Your solution works perfectly!

Sir its working for me properly i got message: “Timeout expired” in the console tell me sir why?

Nice dudeeeeeeeeeee works fine for me on ionic 5…Great

It doesnt work for me… even with the options, how can i fix it?