我有一个小脚本,从geoplugin获取位置...我想将返回的合作伙伴存储在一个我声明为全局的变量中......但由于某种原因,回调不会读取它。我如何使回调使用全局变量?jquery回调不使用全局变量
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
myLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
}, function() {
noLocation();
},{timeout: 20});
}
else if (google.gears) {
var geo = google.gears.factory.create('beta.geolocation');
geo.getCurrentPosition(function(position) {
myLocation = new google.maps.LatLng(position.latitude,position.longitude);
}, function() {
noLocation();
});
}
else {
noLocation();
}
function noLocation() {
$.getJSON("http://www.geoplugin.net/json.gp?id=117.201.92.17&jsoncallback=?",
function(data) {
if (data != "" && data.geoplugin_latitude != "")
myLocation = new google.maps.LatLng(data.geoplugin_latitude, data.geoplugin_longitude)
else
myLocation = new google.maps.LatLng()
});
}
出于某种原因,我想是因为异步调用它的...我可以克服这个问题一些如何?
哪些变量是你谈论你的意思做的“它不会读它”? – casablanca 2011-01-22 21:06:52