用Javascript返回值
我有一个(希望非常简单)的Javascript问题。我搜索过但没有发现与问题真正相关的内容。用Javascript返回值
基本上我有一个函数(addToGlobe),它在运行时调用两个其他函数(codeAddressLat和codeAddressLng)。这两个被调用的函数都应该返回一个浮点值给第一个函数,然后使用它们。子函数的工作是正确的 - 我做了一个print语句来检查每个函数中的“numfinal”变量是否有值,并且它的确如此。
但是,当我向调用函数添加打印语句(如代码中所述)时,它返回'undefined'。因此,问题似乎是在返回numfinal值时。
谢谢:)
function addToGlobe(uname, uid, pmcity) {
// Get lat & long of city
var pmlat = codeAddressLat(pmcity);
var pmlong = codeAddressLng(pmcity);
log(pmlat); // PROBLEM! Prints 'undefined'
log(pmlong); // PROBLEM! Prints 'undefined'
// Rest of function removed to keep it simple
}
function codeAddressLat(inputcity) {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
geocoder.geocode({ 'address': inputcity}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var llsplit = new Array();
bkresult = String(results[0].geometry.location);
bkresult = bkresult.replace(/[\(\)]/g, "");
llsplit = bkresult.split(',');
numfinal = parseFloat(llsplit[0]);
return numfinal;
} else {
log('<b><font color="#C40031">Geocode was not successful:</b> ' + status);
}
});
}
function codeAddressLng(inputcity) {
// Basically the same function as above. Removed for simplicity
}
codeAddressLat
实际上并没有返回任何东西。它传递给geocoder.geocode
的匿名函数是。
由于geocoder.geocode异步运行,因此codeAddressLat不能等待其答案。所以codeAddressLat真的不能返回任何有价值的东西。相反,codeAddressLat也需要变得异步。这是JavaScript中的一种常见模式。
function addToGlobe(uname, uid, pmcity) {
codeAddressLat(pmcity, function(pmlat) {
// do something with pmlat
});
...
}
function codeAddressLat(inputcity, callback) {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
geocoder.geocode({ 'address': inputcity}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var llsplit = new Array();
bkresult = String(results[0].geometry.location);
bkresult = bkresult.replace(/[\(\)]/g, "");
llsplit = bkresult.split(',');
numfinal = parseFloat(llsplit[0]);
// instead of returning, call the callback with the result
callback(numfinal);
} else {
log('<b><font color="#C40031">Geocode was not successful:</b> ' + status);
}
});
}
您没有return语句在codeAddressLat
,你有一个内部codeAddressLat
定义的回调函数内。
您的函数codeAddressLat
实际上并不是返回一个值,而是调用传递一个正在返回值的回调函数的函数。您需要等到地理编码操作完成才能检索numfinal的值。
您的函数codeAddressLat
根本没有返回任何值,因此您将得到未定义的输出。
codeAddressLat根本没有返回任何东西(这意味着它默认返回undefined)。 return语句不在codeAddressLat本身中。 – 2011-04-05 17:16:40
@Matt - 是的,你是对的。错过了看到封闭的功能。谢谢 – 2011-04-05 17:19:19
codeAddressLat方法调用中的地理编码请求不会将值返回给调用方。你的回报是不同的范围。
这是行不通的吗?
function codeAddressLat(inputcity) {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
return geocoder.geocode({ 'address': inputcity}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var llsplit = new Array();
bkresult = String(results[0].geometry.location);
bkresult = bkresult.replace(/[\(\)]/g, "");
llsplit = bkresult.split(',');
numfinal = parseFloat(llsplit[0]);
return numfinal;
} else {
log('<b><font color="#C40031">Geocode was not successful:</b> ' + status);
}
});
}
这geocoder.geocode
功能看起来是异步的。您的codeAddressLat
和codeAddressLng
函数在geocoder.geocode
函数从服务器获取数据之前返回void。
解决此问题的一种方法是将您的呼叫嵌套到geocoder.geocode
并使用变量范围设定,以便所有AJAX呼叫都返回后,您可以调用addToGlobe
函数传递所需的两个参数。
事情是这样的:
codeAddressLatAndLong(pmcity);
function addToGlobe(pmlatlat, pmlatlong) {
log(pmlat); // PROBLEM! Prints 'undefined'
log(pmlong); // PROBLEM! Prints 'undefined'
// Rest of function removed to keep it simple
}
function codeAddressLatAndLong(inputcity) {
// stuff
geocoder.geocode({ 'address': inputcity}, function(results, status) {
// stuff goes here
pmlat = parseFloat(llsplit[0]);
geocoder.geocode({...}, function(results, status) {
// more stuff
pmlatlong = something;
addToGlobe(pmlatlat, pmlatlong);
});
});
}
欢迎AJAX的世界。
很好,非常感谢! :) – 2011-04-05 17:37:41