Reputation: 11
so i want to show the result of geopositioning in my template view.I get the error "cannot read property coords of undefined" Below are the codes
getGeolocationPosition() {
this.geolocation.getCurrentPosition().then((resp) => {
var lati = resp.coords.latitude;
var longi = resp.coords.longitude;
// let pos = {
// lat: resp.coords.latitude,
// lng: resp.coords.longitude,
// };
console.log(lati, longi);
}).catch((error) =>{
console.log('Error getting location' + JSON.stringify(error) );
});
}
<ion-card>
<ion-card-header>What's your current location?</ion-card-header>
<ion-card-content>
<button ion-button round (click)="getGeolocationPosition()">Get location</button>
<ion-input readonly="true" placeholder="Latitude">{{resp.coords.latitude}}</ion-input>
<ion-input readonly="true" placeholder="Longitude"></ion-input>
</ion-card-content>
</ion-card>
Upvotes: 1
Views: 92
Reputation: 1622
Give the class a property called geoResult and set it to the resp object from the async ionic native call.
this.geoResult = resp:
In your html, interpolate using geoResult
{{ geoResult.coords.latitude }}
Upvotes: 1