Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.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
如何从谷歌地图JavaScript地理编码器返回经度和纬度?_Javascript_Google Maps_Geocoding - Fatal编程技术网

如何从谷歌地图JavaScript地理编码器返回经度和纬度?

如何从谷歌地图JavaScript地理编码器返回经度和纬度?,javascript,google-maps,geocoding,Javascript,Google Maps,Geocoding,当我使用下面的代码时,它会提示一个空值吗?为什么呢 HTML 因为您的函数codeAddress被执行,将空数组分配给loc,将异步请求执行给google geocoder,并返回loc,loc为空,因为当来自google的响应到来时,它的实际值被分配。换句话说,allert应该在响应处理程序中: var geocoder; var map; function initialize() { geocoder = new google.maps.Geocoder(); va

当我使用下面的代码时,它会提示一个空值吗?为什么呢

HTML

因为您的函数codeAddress被执行,将空数组分配给loc,将异步请求执行给google geocoder,并返回loc,loc为空,因为当来自google的响应到来时,它的实际值被分配。换句话说,allert应该在响应处理程序中:

var geocoder;
var map;


  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  }

  function codeAddress() {
    var address = document.getElementById("address").value;
    var loc=[];

    // next line creates asynchronous request
    geocoder.geocode( { 'address': address}, function(results, status) {
      // and this is function which processes response
      if (status == google.maps.GeocoderStatus.OK) {
        loc[0]=results[0].geometry.location.lat();
        loc[1]=results[0].geometry.location.lng();

        alert( loc ); // the place where loc contains geocoded coordinates

      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });

    // pretty meaningless, because it always will be []
    // this line is executed right after creating AJAX request, but not after its response comes
    return loc;
  }

  function display(){
     codeAddress();
  }
  function codeAddress() {
    var address = document.getElementById("address").value;

    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        var loc=[]; // no need to define it in outer function now
        loc[0]=results[0].geometry.location.lat();
        loc[1]=results[0].geometry.location.lng();

        display( loc ); 

      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });

  }

  function display( long_lat ){
     alert(long_lat);
  }
这就是AJAX的工作原理。。。进程将生成回调处理程序

如果要将地理编码和“dispaling”分开,可以在处理程序内执行显示功能:

var geocoder;
var map;


  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  }

  function codeAddress() {
    var address = document.getElementById("address").value;
    var loc=[];

    // next line creates asynchronous request
    geocoder.geocode( { 'address': address}, function(results, status) {
      // and this is function which processes response
      if (status == google.maps.GeocoderStatus.OK) {
        loc[0]=results[0].geometry.location.lat();
        loc[1]=results[0].geometry.location.lng();

        alert( loc ); // the place where loc contains geocoded coordinates

      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });

    // pretty meaningless, because it always will be []
    // this line is executed right after creating AJAX request, but not after its response comes
    return loc;
  }

  function display(){
     codeAddress();
  }
  function codeAddress() {
    var address = document.getElementById("address").value;

    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        var loc=[]; // no need to define it in outer function now
        loc[0]=results[0].geometry.location.lat();
        loc[1]=results[0].geometry.location.lng();

        display( loc ); 

      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });

  }

  function display( long_lat ){
     alert(long_lat);
  }
html:


感谢您的回答,有没有办法将这些值返回到变量?bcos我打算在另一个函数中使用它作为参数这仍然不能解决我遇到的问题,我计划获取多个邮政编码的返回值。基本上,我试图创建一个通用函数,在该函数中,我可以将邮政编码作为参数传递,并获取long和lat值作为返回值。由于这些值在回调中,我猜它无法返回。@manraj82:唯一可以编写return loc的地方;可以是传递给geocoder.geocode函数的匿名函数。这个匿名函数由ajax响应处理器在google脚本中的某个地方执行,因此这些信息永远不会出现在您的代码中。AJAX可以通过执行回调来工作,但不能返回值。。。这超出了你的控制
<input type="button" value="Encode" onclick="codeAddress()">
function codeAddress( callback ) {
...
 geocoder.geocode( { 'address': address}, function(results, status) {
   ...
   callback( loc ); // instead of dispaly( loc );
   ...
 }
...
}

codeAddress( display ); // to execute geocoding