要求用户在IONIC 3应用程序中激活Geolocation

时间:2017-08-19 15:57:57

标签: android ios ionic-framework ionic3 ionic-native

我正在使用 Ionic 3 原生Geolocation插件,使用函数"getCurrentPosition()"获取用户位置。但是当我的设备上没有激活地理定位时,我得到了最后保存的位置......

如何让用户在应用程序的开头激活他们的android / ios地理位置?

2 个答案:

答案 0 :(得分:1)

我有同样的问题,所以我使用Toast如下:

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

答案 1 :(得分:0)

我找到了一种更精确的方式:https://forum.ionicframework.com/t/how-to-check-if-geolocation-is-enable-or-not-with-native-gps/88762/4

使用2个离子原生插件: @离子天然/诊断 @离子天然/位置的精确度