I have problem with my flow, I am using promises to do that
The context is : The User click to button to get your position with ionic geolocation, ir returns lat and log , then I want to decode the coordinates to get the City and the last step is set lat,long and city to 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've tried add this.decodeCoord(this.lat, this.long); on uploadLocation but it doesnt function also.
The city is always empty.