Following code is to track user location,
var curr_loc; //global varible
function displayLocation(loc) {
currLat = loc.coords.latitude;
currLon = loc.coords.longitude;
curr_loc = currLat + "," + currLon;
alert(curr_loc); //Displaying latitude and longitude values
}
function foo() {
navigator.geolocation.getCurrentPosition(displayLocation);
alert(curr_loc); //undefined
}
foo();
How can I access location values in foo method.
navigator.geolocation.getCurrentPosition(displayLocation)returns before the position is established, anddisplayLocationwould be called at some point in the future when the position is available.curr_locis undefined for some time afternavigator.geolocation.getCurrentPosition()returns, becausedisplayLocationhas not been called yet.