如何从我正在使用的result()回调函数中捕获返回值?

问题描述:

<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()回调函数中捕获返回值?

你不行。您必须以这样的方式编写代码,以便在回调中执行需要值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);