Value returning as undefined

Issue

I need to get my latitude and longitude. Inside my function, it returns very well, but if i try to call the same this.latitude but out of function, it returns as undefined.

localizar() {

this.geolocation.getCurrentPosition().then((position) > {

  this.latitude  position.coords.latitude;
  this.longitude  position.coords.longitude;

  // Here it returns
  console.log('teste: ', this.latitude, this.longitude);

})
// Here returns as undefined
console.log('lat: ', this.latitude, 'lng: ', this.longitude)

}

Solution

async localizar() {
  const position  await this.geolocation.getCurrentPosition();
  this.latitude  position.coords.latitude;
  this.longitude  position.coords.longitude;

  // your logic here
}

Answered By – John Rosa

Leave a Comment