从服务返回的地理位置值未定义
Geolocation value retuned from service is undefined
我已将地理定位调用放置在我想在应用程序的不同区域使用的服务中。但是,undefined 正在被 returned。如何正确 return 值以便可以检索它?
服务代码
getUserPosition() {
this.geolocation.getCurrentPosition().then((position) => {
this.latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
this.currentLat = position.coords.latitude;
this.currentLng = position.coords.longitude;
return this.latLng;
}, (err) => {
console.log('There was an error');
});
}
从服务中调用此函数
async ngOnInit() {
// Since ngOnInit() is executed before `deviceready` event,
// you have to wait the event.
await this.platform.ready();
await this.loadMap();
this.val = await this.location.getUserPosition();
console.log("I should be here val "+ this.val);
}
您似乎没有在 getUserPosition
函数中 returning 任何内容。你看,return this.latLng;
是 then
in promise 的结果,而不是 getUserPosition
的结果。要解决此问题,例如,您可以等到 promise 完成,然后 return value:
async getUserPosition() {
try {
const position = await this.geolocation.getCurrentPosition();
this.latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
this.currentLat = position.coords.latitude;
this.currentLng = position.coords.longitude;
return this.latLng;
}, (err) => {
console.log('There was an error');
});
}
我强烈建议使用打字稿来避免此类错误 - 如果您正确地向函数添加类型,编译器会告诉您该函数应该 return 特定类型但会尝试 return void
.
正在从函数返回详细信息
async getUserPosition() {
await this.geolocation.getCurrentPosition().then((position) => {
this.latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
this.currentLat = position.coords.latitude;
this.currentLng = position.coords.longitude;
this.locationData = [this.latLng, this.currentLat, this.currentLng];
}, (err) => {
console.log('There was an error' + err.message);
});
// return all the details returned from the location
return this.locationData;
}
使用返回的详细信息
this.geolocationData = await this.location.getUserPosition();
this.latLng = this.geolocationData[0];
this.currentLat = this.geolocationData[1];
this.currentLng = this.geolocationData[2];
我已将地理定位调用放置在我想在应用程序的不同区域使用的服务中。但是,undefined 正在被 returned。如何正确 return 值以便可以检索它?
服务代码
getUserPosition() {
this.geolocation.getCurrentPosition().then((position) => {
this.latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
this.currentLat = position.coords.latitude;
this.currentLng = position.coords.longitude;
return this.latLng;
}, (err) => {
console.log('There was an error');
});
}
从服务中调用此函数
async ngOnInit() {
// Since ngOnInit() is executed before `deviceready` event,
// you have to wait the event.
await this.platform.ready();
await this.loadMap();
this.val = await this.location.getUserPosition();
console.log("I should be here val "+ this.val);
}
您似乎没有在 getUserPosition
函数中 returning 任何内容。你看,return this.latLng;
是 then
in promise 的结果,而不是 getUserPosition
的结果。要解决此问题,例如,您可以等到 promise 完成,然后 return value:
async getUserPosition() {
try {
const position = await this.geolocation.getCurrentPosition();
this.latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
this.currentLat = position.coords.latitude;
this.currentLng = position.coords.longitude;
return this.latLng;
}, (err) => {
console.log('There was an error');
});
}
我强烈建议使用打字稿来避免此类错误 - 如果您正确地向函数添加类型,编译器会告诉您该函数应该 return 特定类型但会尝试 return void
.
正在从函数返回详细信息
async getUserPosition() {
await this.geolocation.getCurrentPosition().then((position) => {
this.latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
this.currentLat = position.coords.latitude;
this.currentLng = position.coords.longitude;
this.locationData = [this.latLng, this.currentLat, this.currentLng];
}, (err) => {
console.log('There was an error' + err.message);
});
// return all the details returned from the location
return this.locationData;
}
使用返回的详细信息
this.geolocationData = await this.location.getUserPosition();
this.latLng = this.geolocationData[0];
this.currentLat = this.geolocationData[1];
this.currentLng = this.geolocationData[2];