0

我尝试学习 IONIC 5。我使用本地地理定位来返回纬度和经度。从这个函数中,我需要检索经纬度,然后通过表格发送到服务器。

geolocate()
   {
         this.geolocation.getCurrentPosition().then((resp) => {
         let position = {
           latitude: resp.coords.latitude,
           longitude: resp.coords.longitude
         }
         return position;
    }).catch((error) => {
      console.log('Error getting location', error);
    });
  }

使用这个其他功能

login(form: NgForm) {
        this.geolocate();
        this.authService.login(form.value.email, form.value.password, this.latitude, this.longitude).subscribe(
            data => {
                this.alertService.presentToast("Logged In");
            },
            error => {
                console.log(error);
            },
            () => {
                this.dismissLogin();
                this.navCtrl.navigateRoot('');
            }
        );
    }
4

2 回答 2

0
async login(form: NgForm) {
        let res = await this.geolocate();
        this.latitude = res.latitude;
        this.longitude = res.longitude;
        this.authService.login(form.value.email, form.value.password, this.latitude, this.longitude).subscribe(
            data => {
                this.alertService.presentToast("Logged In");
            },
            error => {
                console.log(error);
            },
            () => {
                this.dismissLogin();
                this.navCtrl.navigateRoot('');
            }
        );
    }
于 2020-03-18T09:59:09.770 回答
0

您必须将呼叫返回到getCurrentPosition()

geolocate() {

return this.geolocation.getCurrentPosition().then((resp) => {
     return {
       latitude: resp.coords.latitude,
       longitude: resp.coords.longitude
     };
   }).catch((error) => {
    console.log('Error getting location', error);
   });
}

然后,等待结果并将其存储在一个变量中,以便在下一个函数调用中使用它。

async login(form: NgForm) {
    const coords = await this.geolocate();

this.authService.login(form.value.email, form.value.password, coords.latitude, coords.longitude).subscribe(
        data => {
            this.alertService.presentToast("Logged In");
        },
        error => {
            console.log(error);
        },
        () => {
            this.dismissLogin();
            this.navCtrl.navigateRoot('');
        }
    );
}
于 2020-03-19T01:58:54.337 回答