I attempted to retrieve my location by saving the latitude and longitude, but my declared variable isn't returning anything.
Take a look at my code snippet:
public device_location: any = {};
constructor(private geolocation: Geolocation) {
this.setGeolocation();
this.getGeolocation();
}
setGeolocation(){
this.geolocation.getCurrentPosition().then((resp) => {
// resp.coords.latitude
this.device_location.latitude = resp.coords.latitude;
this.device_location.longitude = resp.coords.longitude;
console.log( this.device_location.longitude);
}).catch((error) => {
console.log('Error getting location', error);
});
let watch = this.geolocation.watchPosition();
watch.subscribe((data) => {
// data can be a set of coordinates, or an error (if an error occurred).
// data.coords.latitude
// data.coords.longitude
});
}
getGeolocation(){
console.log( JSON.stringify(this.device_location) );
return this.device_location;
}
Here is the result of the code:
123.45600 {}