谷歌地理编码器 - 异步请求返回“未定义”值

时间:2014-01-29 09:12:32

标签: javascript ajax google-maps asynchronous

我有这个函数应该从纬度和经度开始返回国家,并将它分配给函数外部的全局变量声明。我知道Ajax中的a代表异步,我已经阅读了stackoverflow上的每一个答案,但我无法修复它。

    function getLatLongDetail(myLatlng) {

    var geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({ 'latLng': myLatlng },
      function (results, status) {
        var country = "";

          if (status == google.maps.GeocoderStatus.OK) {
              if (results[0]) {
                  for (var i = 0; i < results[0].address_components.length; i++) {
                      var addr = results[0].address_components[i];
                      // check if this entry in address_components has a type of country
                      if (addr.types[0] == 'country')
                          country = addr.long_name;

                  }

                  }
                  return country; // this doesn't work

              }

          }

      });
}


var Country = getLatLongDetail(myLatlng);
alert(Country);// undefined

我知道有几百个关于回调函数的问题,但没有一个对我有用。

1 个答案:

答案 0 :(得分:0)

最后我成功了!代码如下:

   function getLatLongDetail(myLatlng, fn) {

    var geocoder = new google.maps.Geocoder(); 
    var country = "";
    var city = "";
    var address = "";
    var zip = "";
    var state = "";


    geocoder.geocode({ 'latLng': myLatlng },
      function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
              if (results[0]) {
                  for (var i = 0; i < results[0].address_components.length; i++) {
                      var addr = results[0].address_components[i];
                      // check if this entry in address_components has a type of country
                      if (addr.types[0] == 'country')
                      country = addr.long_name;
                     else if (addr.types[0] == ['locality'])       // City
                       city = addr.long_name;
                        else if (addr.types[0] == 'street_address') // address 1
                          address = address + addr.long_name;
                      else if (addr.types[0] == 'establishment')
                          address = address + addr.long_name;
                      else if (addr.types[0] == 'route')  // address 2
                          address = address + addr.long_name;
                      else if (addr.types[0] == 'postal_code')       // Zip
                          zip = addr.short_name;
                      else if (addr.types[0] == ['administrative_area_level_1'])       // State
                          state = addr.long_name;

                  }
              }
            fn(country,city, address, zip, state);
          }
      });
   }

   var mCountry; //global variable to store the country 
   vat mCity; //global variable to store the city 

getLatLongDetail(event.latLng, function(country,city, address, zip, state){
    mCountry = country; // now mCountry store the value from  getLatLongDetail so I can save it in the database
    mCity  = city;
    alert(address + zip + state);
   });