【发布时间】:2017-07-03 11:42:50
【问题描述】:
我对 Nativescript(使用 angular2/typescript)非常陌生。我的用例是使用 nativescript 地理定位插件跟踪用户位置并保存结果(例如纬度和经度)以供以后使用。下面是我的示例代码:
export class AppComponent {
public latitude: number;
public longitude: number;
public constructor()
{
this.updateLocation();
this.getWeather(this.latitude ,this.longitude);
}
private getDeviceLocation(): Promise<any> {
return new Promise((resolve, reject) => {
geolocation.enableLocationRequest().then(() => {
geolocation.getCurrentLocation({desiredAccuracy:3,updateDistance:10,timeout: 20000}).then(location => {
resolve(location);
}).catch(error => {
reject(error);
});
});
});
}
public updateLocation() {
this.getDeviceLocation().then(result => {
// i am saving data here for later usage
this.latitude = result.latitude;
this.longitude = result.longitude;
}, error => {
console.error(error);
});
}
public getWeather(latitude:number,longitude:number){
// do stuff with lat and long
}
}
但我无法将纬度和经度的值传递给 getWeather 方法。它是未定义的。我做错了什么?我知道解决方法:通过从这些值可用的 updateLocation 内部调用 getWeather 并让这个东西正常工作,但不知何故我觉得这不是一种合适的方式。提前致谢。
【问题讨论】:
-
getWeather()函数将在updateLocation()方法有机会完成之前触发,因此值将是未定义的
标签: typescript geolocation nativescript angular2-nativescript