I try to get my current browser location and set it in variables. If browser doesn't have activated geolocation set a default latitude and longitude.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(location) {
this.latitude = location.coords.latitude;
this.longitude = location.coords.longitude;
});
}else{
this.latitude = 43.318820;
this.longitude = -3.004561
}
console.log(this.latitude); //Undefinied
console.log(this.longitude);//Undefinied
The problem: Variables return undefinied in every cases. So, ¿What´s wrong in this code?
EDIT
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(location => {
this.latitude = location.coords.latitude;
this.longitude = location.coords.longitude;
});
}else{
this.latitude = 43.318820;
this.longitude = -3.004561
}
console.log(this.latitude);
console.log(this.longitude);