首页 > 解决方案 > IONIC中的返回值形式函数

问题描述

我尝试学习 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('');
            }
        );
    }

标签: angularionic-frameworkionic5

解决方案


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

推荐阅读