I'm trying to get a object from my getCurrentLocation() method and then I would like to use the coordinates (position.coords.latitude, position.coords.longitude) of this object on my getUserAddressBy() method. I'm calling it from my app.js, but when I console log there, it gets an undefined value, and when I console log directly on the method I get the right object.
Any help, please? I'm totally new on JavaScript programming.
Here is my code.
class GeoLocator {
constructor(city, country) {
this.keyUser = "";
this.city = city;
this.country = country;
}
async getUserAddressBy(lat, long) {
const location = await fetch(`https://maps.googleapis.com/maps/api/geocode/json?
latlng=${lat},${long}&key=${this.keyUser}`);
const currentLoc = await location.json();
this.city = currentLoc.results[0].address_components[3].long_name;
this.country = currentLoc.results[0].address_components[5].short_name;
return { city: this.city, country: this.country };
}
getCurrentLocation() {
self = this;
return navigator.geolocation.getCurrentPosition(position => {
console.log(position);
return position;
});
}
}
// My app.js file
const currentLocation = new GeoLocator();
console.log(currentLocation.getCurrentLocation());