I am completely new to Nativescript (with Angular 2/TypeScript). My goal is to utilize the Nativescript geolocation plugin to track a user's location and store the data (latitude and longitude) for future use. Here is a snippet of my code:
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 => {
// saving data here for later use
this.latitude = result.latitude;
this.longitude = result.longitude;
}, error => {
console.error(error);
});
}
public getWeather(latitude:number,longitude:number){
// perform actions with latitude and longitude
}
}
However, I am facing an issue where I cannot pass the values of latitude and longitude to the getWeather method. They are being received as undefined. What could be the reason for this? I am aware of a workaround by calling getWeather directly inside the updateLocation function where these values are available, but I feel like there might be a better way to handle this. Any help would be greatly appreciated.