Javascript 如何从我';我在用什么? var geo=new GClientGeocoder(); 函数showAddress(){ var search=document.getElementById(“search”).value; //getLocations没有ret,所以wtf! geo.getLocations(搜索,函数(结果){(result.Status.code==200)?警报(result.Placemark[0].Point.coordinates):警报(result.Status.code);}); }

Javascript 如何从我';我在用什么? var geo=new GClientGeocoder(); 函数showAddress(){ var search=document.getElementById(“search”).value; //getLocations没有ret,所以wtf! geo.getLocations(搜索,函数(结果){(result.Status.code==200)?警报(result.Placemark[0].Point.coordinates):警报(result.Status.code);}); },javascript,callback,anonymous-function,Javascript,Callback,Anonymous Function,我需要回调的ret值,因为getLocations()不返回任何值。我该怎么做?你不能。您必须以这样的方式编写代码:需要结果值的代码在回调中执行 示例(我刚刚以一种我觉得合乎逻辑的方式命名了函数): <script type="text/javascript"> var geo = new GClientGeocoder(); function showAddress() { var search = document.getElementById("search"

我需要回调的ret值,因为getLocations()不返回任何值。我该怎么做?

你不能。您必须以这样的方式编写代码:需要
结果
值的代码在回调中执行

示例(我刚刚以一种我觉得合乎逻辑的方式命名了函数):

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