我试着学习IONIC 5。我使用本地地理定位来返回纬度和经度。从这个功能,我需要检索lat和long,然后通过表格发送到服务器
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('');
}
);
}
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('');
}
);
}
您必须将调用返回给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('');
}
);
}