0

I'm using the Ionic 3 native Geolocation plugin to get the user position using the function "getCurrentPosition()". But when the geolocation is not activated on my device I get the last saved position...

How can I ask users to activate their android/ios geolocation in the start of the app?

Saurabh Jain
  • 1,688
  • 14
  • 28
adilmrk
  • 27
  • 9

2 Answers2

1

I have the same problem, so I used a Toast as following:

import { ToastController } from 'ionic-angular';
...
  constructor(private geolocation: Geolocation,
          private toastCtrl: ToastController) {
...

let toast = this.toastCtrl.create({
   message: 'Your device location is not available. Please turn on your GPS.',
   position: 'middle',
   showCloseButton: true,
   closeButtonText: 'OK'
});
    
this.geolocation.getCurrentPosition().then((resp) => {
   //if location available - do whatever you need
}).catch((error) => {
   toast.present();
   console.log('Error getting location', error);
});
}
RafaelJan
  • 3,118
  • 1
  • 28
  • 46
  • but this way the user can tap the "Ok" button and still use the app without activating geolocation – adilmrk Aug 19 '17 at 22:29
0

I found a more exact way : https://forum.ionicframework.com/t/how-to-check-if-geolocation-is-enable-or-not-with-native-gps/88762/4

using 2 ionic native plugins : @ionic-native/diagnostic @ionic-native/location-accuracy

adilmrk
  • 27
  • 9