2011-04-05 71 views
0

我有一個(希望非常簡單)的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 
    } 

回答

1

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); 
     } 
    }); 
} 
+0

很好,非常感謝! :) – 2011-04-05 17:37:41

0

您沒有return語句在codeAddressLat,你有一個內部codeAddressLat定義的回調函數內。

0

您的函數codeAddressLat實際上並不是返回一個值,而是調用傳遞一個正在返回值的回調函數的函數。您需要等到地理編碼操作完成才能檢索numfinal的值。

0

您的函數codeAddressLat根本沒有返回任何值,因此您將得到未定義的輸出。

+0

codeAddressLat根本沒有返回任何東西(這意味着它默認返回undefined)。 return語句不在codeAddressLat本身中。 – 2011-04-05 17:16:40

+0

@Matt - 是的,你是對的。錯過了看到封閉的功能。謝謝 – 2011-04-05 17:19:19

0

codeAddressLat方法調用中的地理編碼請求不會將值返回給調用方。你的回報是不同的範圍。

0

這是行不通的嗎?

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); 
     } 
    }); 
} 
0

geocoder.geocode功能看起來是異步的。您的codeAddressLatcodeAddressLng函數在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的世界。