The first method returns promise.
getCoordinates() {
return new Promise(function(resolve, reject) {
navigator.geolocation.getCurrentPosition(resolve, reject);
});
}
Returns the result of reverseGeoCode
method.
async getAddress() {
await this.getCoordinates().then(position => {
let latitude = position.coords.latitude;
let longitude = position.coords.longitude;
let url = Constants.OSMAP_URL + latitude + "&lon=" + longitude;
// Reverse geocoding using OpenStreetMap
return this.reverseGeoCode(url);
});
}
Uses custom class to make an API call and return the result.
reverseGeoCode(url) {
let requestService = new RequestService("json", url);
requestService.call().then(result => {
return result;
});
}
This is how I call:
let geoLocation = new GeoLocation();
geoLocation.getAddress().then(r => {
console.log(r);
});
The console logs undefined.
getAddress
has noreturn
statement, so of course it returns a Promise that resolves asundefined
– Monopolistreturn this.reverseGeoCode(url);
this should be outside theawait
? – Messmatethen
to outside? – Messmate