Hi! I try to save the latitude and longitude in an object. When I use console.log for the first time my variable contains the value of the latitude and longitude but in the second console.log, my value doesn’t appear. What is the problem? I read that it is for async programming but I don’t get it.
I read that from https://stackoverflow.com/questions/9935059/saving-variables-outside-of-navigator-geolocation-getcurrentposition-javascrip
$(document).ready(function(){
var userPosition =
{
lat: '',
lon: ''
};
getLocation();
function getLocation()
{
if(navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(function(position){
userPosition.lat = position.coords.latitude;
userPosition.lon = position.coords.longitude;
console.log(userPosition.lat); //This works!
});
}
else
{
alert("Geolocation not supported by your browser");
}
}
console.log(userPosition.lat); // This not works
});