umsuka
September 3, 2019, 12:43pm
1
Hi
I would like assistance with getting city name from gps coords, I am using agm/core.
Here is my code that gets gps lat and long and its working on getting coords, I need to get a city name with the coords returned.
onLocate() {
const loader = this.loadingCtrl.create({
content: 'Getting your Location...'
});
loader.present();
this.geolocation
.getCurrentPosition()
.then((location) => {
loader.dismiss();
this.location.lat = location.coords.latitude;
this.location.lng = location.coords.longitude;
})
.catch((error) => {
loader.dismiss();
const toast = this.toastCtrl.create({
message: 'Could not get location, please pick it manually!',
duration: 2500
});
toast.present();
});
}
getGeolocation() {
this.geolocation.getCurrentPosition().then((resp) => {
this.latitude = resp.coords.latitude;
this.longitude = resp.coords.longitude;
this.accuracy = resp.coords.accuracy;
this.getGeoencoder(resp.coords.latitude, resp.coords.longitude);
}).catch((error) => {
alert('Error getting location' + JSON.stringify(error));
});
}
//geocoder method to fetch address from coordinates passed as arguments
getGeoencoder(latitude, longitude) {
this.nativeGeocoder.reverseGeocode(latitude, longitude, this.geoencoderOptions)
.then((result: NativeGeocoderResult[]) => {
this.address = this.generateAddress(result[0]);
})
.catch((error: any) => {
alert('Error getting location' + JSON.stringify(error));
});
}
//Return Comma saperated address
generateAddress(addressObj) {
let obj = [];
let address = "";
for (let key in addressObj) {
obj.push(addressObj[key]);
}
obj.reverse();
for (let val in obj) {
if (obj[val].length)
address += obj[val] + ', ';
}
return address.slice(0, -2);
}