2011-03-15 122 views
0
<script type="text/javascript"> 
    var geo = new GClientGeocoder(); 

    function showAddress() { 
    var search = document.getElementById("search").value; 
    // getLocations has not ret, so wtf! 
    geo.getLocations(search, function (result) { (result.Status.code == 200) ? alert(result.Placemark[0].Point.coordinates) : alert(result.Status.code); }); 
    }</script> 

我需要回調的ret值作爲getLocations()返回沒有值。我怎樣才能做到這一點?如何從我正在使用的result()回調函數中捕獲返回值?

回答

2

你不行。您必須以這樣的方式編寫代碼,以便在回調中執行需要值result的代碼。

例(我剛剛入選其中似乎是合乎邏輯我函數的方式):

function drawPlacemarks(marks) { 
    // do fancy stuff with the results 
    alert(marks[0].Point.coordinates); 
} 

function getAddress(callback) { 
    var search = document.getElementById("search").value; 
    geo.getLocations(search, function (result) { 
     if(result.Status.code == 200) { 
      // pass the result to the callback 
      callback(result.Placemark); 
     } 
    }); 
} 

getAddress(drawPlacemarks); 
相關問題