Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Google Maps JavaScript geocoder回调返回变量?_Javascript_Google Maps_Asynchronous - Fatal编程技术网

如何从Google Maps JavaScript geocoder回调返回变量?

如何从Google Maps JavaScript geocoder回调返回变量?,javascript,google-maps,asynchronous,Javascript,Google Maps,Asynchronous,我正在使用GoogleMapsAPI,每当我将变量从codeLatLng函数返回到initialize函数时,它都声称未定义。如果我从codeLatLng打印变量,它会显示得很好 var geocoder; function initialize() { geocoder = new google.maps.Geocoder(); var latlng = new google.maps.LatLng(40.730885,-73.997383); var addr

我正在使用GoogleMapsAPI,每当我将变量从codeLatLng函数返回到initialize函数时,它都声称未定义。如果我从codeLatLng打印变量,它会显示得很好

  var geocoder;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(40.730885,-73.997383);
    var addr = codeLatLng();
    document.write(addr);

  }

  function codeLatLng() {
    var latlng = new google.maps.LatLng(40.730885,-73.997383);
    if (geocoder) {
      geocoder.geocode({'latLng': latlng}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          if (results[1]) {
                return results[1].formatted_address;
          } else {
            alert("No results found");
          }
        } else {
          alert("Geocoder failed due to: " + status);
        }
      });
    }
  }
打印未定义的内容

如果我这样做:

  var geocoder;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(40.730885,-73.997383);
    codeLatLng();


  }

  function codeLatLng() {
    var latlng = new google.maps.LatLng(40.730885,-73.997383);
    if (geocoder) {
      geocoder.geocode({'latLng': latlng}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          if (results[1]) {
                document.write(results[1].formatted_address);
          } else {
            alert("No results found");
          }
        } else {
          alert("Geocoder failed due to: " + status);
        }
      });
    }
  }

打印出美国纽约州纽约市纽约市10012号

返回的数据不是从
codeLatLng
返回的;它从传递给
geocoder.geocode
的匿名函数返回


我认为您需要使用另一种机制来传递数据,例如全局变量

将geocoder作为参数传递给codeLatLng()函数

function codeLatLng(geocoder) {
在初始化函数中这样调用:

var addr = codeLatLng(geocoder);
var geocoder;
function initialize() {
  geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(40.730885,-73.997383);
  codeLatLng(function(addr){
    alert(addr);
  });
}

function codeLatLng(callback) {
  var latlng = new google.maps.LatLng(40.730885,-73.997383);
  if (geocoder) {
    geocoder.geocode({'latLng': latlng}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        if (results[1]) {
          callback(results[1].formatted_address);
        } else {
          alert("No results found");
        }
      } else {
        alert("Geocoder failed due to: " + status);
      }
    });
  }
}

您正在发出一个异步请求,您的
codeLatLng()
函数在地理编码器完成之前很久就已经完成并返回了

如果需要geocoder数据继续,则必须将功能链接在一起:

function initialize() {
    geocoder = new google.maps.Geocoder();
    codeLatLng();
}
function codeLatLng() {
    var latlng = new google.maps.LatLng(40.730885,-73.997383);
    if (geocoder) {
        geocoder.geocode({'latLng': latlng}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                    if (results[1]) {
                        initContinued(results[1].formatted_address);
                    } else {
                        alert("No results found");
                    }
                } else {
                    alert("Geocoder failed due to: " + status);
                }
        });
      }

}
function initContinued(addr) {
    alert(addr);
}

不能从函数返回值,函数返回时该值还不存在

geocode
方法进行异步调用并使用回调来处理结果,因此您必须在
codeLatLng
函数中执行相同的操作:

var addr = codeLatLng(geocoder);
var geocoder;
function initialize() {
  geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(40.730885,-73.997383);
  codeLatLng(function(addr){
    alert(addr);
  });
}

function codeLatLng(callback) {
  var latlng = new google.maps.LatLng(40.730885,-73.997383);
  if (geocoder) {
    geocoder.geocode({'latLng': latlng}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        if (results[1]) {
          callback(results[1].formatted_address);
        } else {
          alert("No results found");
        }
      } else {
        alert("Geocoder failed due to: " + status);
      }
    });
  }
}

您可以使用localstorage获取价值

 geocoder.geocode({
            'address': address,             
        }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var latitude = results[0].geometry.location.lat();
                var longitude = results[0].geometry.location.lng();
            }                             
           localStorage.setItem("endlat", latitude);
           localStorage.setItem("endlng", longitude);
            });
var end_lat = localStorage.getItem("endlat");
var end_lng = localStorage.getItem("endlng");

但它返回以前的值。。单击两次后返回当前值…

是否可以返回addr的值?我应该如何将addr分配给变量?@AbtPst:异步调用中不能从结果返回任何内容,因为调用在有结果之前返回。我将
警报(addr)放在哪里是可以使用结果的地方。可以将该值赋给全局变量,但是仍然需要等待该值的到来。是的,这样就可以了。实际上我有一个lat-long值数组。我希望以迭代方式处理数组,并不断将地址添加到列表中。顺便问一下,有没有一种方法可以一次对lat-lon值数组进行反向地理编码?如何将addr赋值给全局变量?我可以在函数中执行x=addr吗?@AbtPst:我不知道是否有处理多个坐标的方法,您必须查阅文档。否则,您将链接调用,即在一个结果到达时再次调用。要收集结果,您可能需要使用全局数组,如
var addresses=[],然后使用
地址推送(addr)以添加结果。其他执行多个调用的用户会受到服务器的限制,即在短时间内完成一定数量的调用后,它会返回一个错误。因此,当您得到该结果时,您需要等待一段时间,然后再尝试。这是最好的方法,因为不必在全局变量中公开地理编码器的结果。