I'm trying to store the location value out of the function's scope. The HTML5 geolocation API is what I need. Here is my code:
var location={};
navigator.geolocation.getCurrentPosition(function maPosition(position) {
location.latitude = position.coords.latitude;
location.longitude = position.coords.longitude;
});
console.log(position.latitude);
console.log(position.longitude);
When I run this, I get the error variable undefined
. This is due to the fact that it might not be available in in the scope of maPosition
. Since I'm a rookie in JavaScript I was wondering what is the way around it. How can I access the value of position.coords
out of the scope it's declared in? Can anyone help?