Ionic 3 地理定位错误

Ionic 3 geolocation error

我正在使用 ionic 本地地理定位插件

https://ionicframework.com/docs/native/geolocation/ 获取用户当前位置。一切正常,除了我在用户关闭手机位置时遇到的问题,即使用户允许位置访问,应用也无法将其重新打开。

        this.options = {
            enableHighAccuracy: true,
            timeout: 10000
        };
        this.geolocation.getCurrentPosition(this.options).then((pos: Geoposition) => {
            this.currentLat = pos.coords.latitude;
            this.currentLon = pos.coords.longitude;

        );
        }, (err: PositionError) => {
            console.log("error : " + err.message);

        })

我似乎无法理解为什么会这样。

如果用户关闭了他的设备位置,那么您需要使用 ionic 本机诊断插件进行检查,如果位置关闭,请将用户带到 locationSettings

this.diagnostic.isGpsLocationEnabled().then(state => {
  if (!state) {
    let confirm = this.alertCtrl.create({
      title: '<b>Location</b>',
      message: 'Location information is unavaliable on this device. Go to Settings to enable Location.',
      buttons: [
        {
          text: 'cancel',
          role: 'Cancel',
          handler: () => {
            this.navCtrl.push(alternatePage); // this is optional(you can use only one button if maps is necessary) according to your needs if you want to navigate user to some other place if he does not give location access.
          }
        },
        {
          text: 'Go to settings,
          handler: () => {
            this.diagnostic.switchToLocationSettings()
          }
        }
      ]
    });
    confirm.present();
  }
  else {
    this.navCtrl.push(yourMapsPage) // if access is available take to maps page. 
  }
})