0

There are so many threads about this topic but I still haven't been able to find a suitable answer for my case...

Here is what I'm trying to do:

1) define a function to get geocodes with Google Maps API v3

2) call the function with the address in question in order to get the geocode for this particular address

The code works perfectly fine if I place

    alert(address); 

after

    address = results[0].geometry.location;

As I need it as a global variable, this is unfortunately not an option. When trying to use the 'address' variable as a global function, I always get as value 'undefined'.

<!DOCTYPE html>
<html>
<head>
  <script src="http://maps.googleapis.com/maps/api/js?sensor=false"
      type="text/javascript"></script>
</head>

  <script type="text/javascript">
    var geocoder;
    var address;

     address = 'Frankfurt, Germany';

  function codeAddress() {
    geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': address}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {

address = results[0].geometry.location;


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

codeAddress(address);
alert(address);

  </script>


</body>
</html>

Anybody able to help me with this? Staring at the code doesn't seem to get me anywhere. I appreciate all help available, thanks!!!

4

2 回答 2

0

问题是geocode异步,不是吗?

这意味着地理编码回调在“稍后的某个时间点”被调用,这是最初调用原始帖子中的外部更改之后。(它不应该警告 undefined 因为它首先设置为特定的字符串,所以我忽略了该语句。)

function codeAddress() {
  geocoder = new google.maps.Geocoder();
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      // Only use address *after the callback success* and get rid of
      // the global variable if possible - note how it's just passed here.
      // You can use `debugger;` to stop here and check the value to
      // make sure it is set as appropriate.
      var address = results[0].geometry.location;
      alert(address);
      doOtherStuffWithAddress(address);
    } else {
      alert("Geocode was not successful for the following reason: " + status);
    }
  });
}

function doOtherStuffWithAddress (address) {
   // don't need no stinking globals
}

请参阅如何从 AJAX 调用返回响应?

AJAX 中的 A 代表异步。这意味着发送请求(或者更确切地说是接收响应)从正常的执行流程中取出。在您的示例中, $.ajax 立即返回,并且下一个语句 return result; 在您作为成功回调传递的函数甚至被调用之前执行。

并专门针对上下文:

地理编码功能是异步的。这意味着发送请求(或者更确切地说是接收响应)从正常的执行流程中取出。在您的示例中,geocode 立即返回,并且在您作为成功回调传递的函数甚至被调用之前执行下一条语句 alert(address)。

于 2013-05-13T20:47:03.537 回答
0

谢谢你们的回答。我现在可以完成我的项目了!!!在下面找到我的最终代码。该代码填充地址数组的地理编码。玩得开心。

<!DOCTYPE html>
<html>
<head>
  <script src="http://maps.googleapis.com/maps/api/js?sensor=false"
          type="text/javascript"></script>
</head>
<BODY>
<DIV ID="pro"></div>
<DIV ID="adr"></div>

  <script type="text/javascript">
    var geocoder;




var addresses = "Berlin, Deutschland; Frankfurt, Deutschland; Broadway Street, New York City, New York, USA";


 geocoder = new google.maps.Geocoder();

var address = addresses.split(';');

var i = 0;
var timeout = 600;


codeAddress(i);


function codeAddress(i) {


    geocoder.geocode( { 'address': address[i]}, function(results, status) 
    {
            if (status == google.maps.GeocoderStatus.OK) {
            document.getElementById("adr").innerHTML = document.getElementById("adr").innerHTML + "<BR>{location: new google.maps.LatLng" + results[0].geometry.location + ", weight: 1},";

                                } else {
                if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT)
                    {
                        setTimeout(function() { addMarker(position); }, (timeout * 3));
                    }
                                        }


i++;
var percent = ((i) / address.length) * 100;
percent = percent.toFixed(2);
percent = percent + " %";
document.getElementById("pro").innerHTML = percent; 


if (i < address.length)
        {
            setTimeout(function() { codeAddress(i); }, (timeout));
         }

    });



            }

</script>


</body>
</html>
于 2013-05-28T11:56:45.177 回答