以下代码用于跟踪用户位置,
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();
如何在 foo 方法中访问位置值。