首页 > 解决方案 > 如何使用科尔多瓦 LocationAccuracy 等待使用 aprove 的地理位置?

问题描述

LocationAccuracy在 Ionic 上使用,当应用程序在没有启用位置功能的情况下打开时出现问题,应用程序请求权限正确但之后他什么也没做

查看应用程序是否获得许可的代码

  autorizacoes(){
    this.diagnostic.isLocationEnabled().then(
      (resp)=>{
        if(resp){
          this.on_load()
        }else{
          this.diagnostic.registerLocationStateChangeHandler(()=>{this.on_load()})
          this.request_location()
        }
      }
    ).catch(err=>{
      this.alertCtrl.create({
        title: 'Erro',
        buttons: [{ text: 'ok' }],
        subTitle: 'Ative o gps para prosseguir'
      }).present();
    })
  }

请求代码

 request_location(){
    this.carregando = true
      this.locationAccuracy.canRequest().then((canRequest: boolean) => {
      if (canRequest) {
        this.status = 'pode pedir'  this.locationAccuracy.request(this.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY).then(
          () => {
            this.status='vai esperar'
            this.on_load()
          },
        ).catch(err=>{
          this.status = 'deu erro'
            this.alertCtrl.create({
              title: 'Erro',
              buttons: [{ text: 'ok' }],
              subTitle: 'Ative o gps para prosseguir'
            }).present();
          this.request_location()

        })
      }
      else{
        this.request_location()
      }

    });
  }

最后是我要在开启位置后运行的代码

  on_load() {
    this.geolocation.getCurrentPosition().then((resp) => {
      this.pegar_orcamentos_diponiveis(this.raio, { lat: resp.coords.latitude, lng: resp.coords.longitude });
      console.log(this.orcamentos.length)
      setTimeout(()=>{
      //this dont run
        this.autorizacoes()
      },30000)
    }).catch((error) => {
      console.log('Error getting location', error);

      this.autorizacoes()
    });
    setTimeout(() => {
    this run
      this.status = 'end of time'
      this.autorizacoes()
    }, 30000)
  }

我放了一个timeout只是为了测试代码的去向。

标签: cordovatypescriptmobileionic3cordova-plugins

解决方案


我通过在发出请求后重新加载页面解决了这个问题,我使用了 window.location.reload()。不是正确的方法,但有效


推荐阅读