I am facing an issue with my flow, where I am utilizing promises to handle the process.
Here is the scenario: The User clicks a button to retrieve their current position using Ionic geolocation, which returns the latitude and longitude. Next, I aim to decode these coordinates to obtain the City, and finally, set the latitude, longitude, and city for the user.
tryGeolocation() {
this.geolocation.getCurrentPosition().then((resp) => {
let pos = {
lat: resp.coords.latitude,
lng: resp.coords.longitude
};
this.lat = resp.coords.latitude;
this.long = resp.coords.longitude;
console.log(this.lat+"--"+this.long);
this.decodeCoord(this.lat, this.long);
alert(this.city);
this.uploadLocation();
}).catch((error) => {
console.log('Error getting location', error);
this.loading.dismiss();
});
}
decodeCoord(lat, long) {
console.log("decodeCoord");
var latlng = new google.maps.LatLng(lat, long);
this.geocoder.geocode({'latLng': latlng}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
var indice = 0;
for (var j = 0; j < results.length; j++) {
if (results[j].types[0] == 'locality') {
indice = j;
break;
}
}
let city, region, country;
for (var i = 0; i < results[j].address_components.length; i++) {
if (results[j].address_components[i].types[0] == "locality") {
//this is the object you are looking for City
city = results[j].address_components[i];
}
if (results[j].address_components[i].types[0] == "administrative_area_level_1") {
//this is the object you are looking for State
region = results[j].address_components[i];
}
if (results[j].address_components[i].types[0] == "country") {
//this is the object you are looking for
country = results[j].address_components[i];
}
}
//city data
this.city=city;
} else {
console.log("No results found");
}
} else {
console.log("Geocoder failed due to: " + status);
}
});
}
uploadLocation() {
let position = {
latitude: this.lat,
longitude: this.long,
city: this.city
}
this._us.updateUserIndividual(position).then(data => {
console.log("USER UPDATED");
}).catch((err) => {
this.presentToast("Ups! Ha ocurrido un Error, intentalo otra vez");
})
}
I have attempted to include
this.decodeCoord(this.lat, this.long);
in uploadLocation()
, but it does not work either.
The issue persists as the city always remains empty.