我有一個(希望非常簡單)的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
}
很好,非常感謝! :) – 2011-04-05 17:37:41