javascript - Google 地理编码 v3 - 获取格式化地址

标签 javascript jquery google-maps google-maps-api-3 geolocation

我想知道是否有人可以帮助我。尽管我似乎能够获取城市、国家/地区,但我很难在地理编码结果的对话框中获取坐标和格式化地址。

这是我正在使用的代码:

http://jsfiddle.net/d5DBh/5/

代码

var myLatlng = new google.maps.LatLng(31.272410, 0.190898);


 // INITALIZATION
 function initialize() {
     var mapOptions = {
         zoom: 4,
         center: myLatlng,
         mapTypeId: google.maps.MapTypeId.ROADMAP
     }
     var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
 }


 // GEOCODE
 function codeAddress() {
     var address = document.getElementById("address").value;
     new google.maps.Geocoder().geocode({
         'address': address
     }, function (results, status) {
         if (status == google.maps.GeocoderStatus.OK) {
             if (results[0]) {
                 var address = "",
                     city = "",
                     town = "",
                     state = "",
                     country = "",
                     location = "",
                     format = "";
                 for (var i = 0; i < results[0].address_components.length; i++) {
                     var addr = results[0].address_components[i];
                     if (addr.types[0] == 'country') country = addr.short_name;
                     else if (addr.types[0] == 'street_address')
                     address = address + addr.long_name;
                     else if (addr.types[0] == 'route')
                     address = address + addr.long_name;
                     else if (addr.types[0] == ['administrative_area_level_1'])
                     state = addr.long_name;
                     else if (addr.types[0] == ['administrative_area_level_2'])
                     town = addr.long_name;
                     else if (addr.types[0] == ['locality'])
                     city = addr.long_name;
                     else if (addr.types[0] == ['location'])
                     location = addr.location;
                 }
                 alert('Formated Address: ' + format + '\n' + 'City: ' + city + '\n' + 'Town: ' + town + '\n' + 'State: ' + state + '\n' + 'Country: ' + country + '\n' + 'Coordinates: ' + location);
             }
     } else {
         alert("Error: We could not find this address - please try and different location");
     };
     });
 }

 initialize();
 document.getElementById("searchItem").onclick = function () {
     codeAddress();
     return false;
 };

此外,由于我是 Javascript 新手,所以我愿意接受任何有关更干净地编写此内容的想法。

最佳答案

function getLatLongDetail(myLatlng) {

        var geocoder = new google.maps.Geocoder(); 
        geocoder.geocode({ 'latLng': myLatlng },
          function (results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                  if (results[0]) {

                      var address = "", city = "", state = "", zip = "", country = "", formattedAddress = "";
                      var lat;
                      var lng;

                      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] == '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;
                          else if (addr.types[0] == ['locality'])       // City
                              city = addr.long_name;
                      }


                      if (results[0].formatted_address != null) {
                          formattedAddress = results[0].formatted_address;
                      }

                      //debugger;

                      var location = results[0].geometry.location;

                      lat = location.lat;
                      lng = location.lng;

                      alert('City: '+ city + '\n' + 'State: '+ state + '\n' + 'Zip: '+ zip + '\n' + 'Formatted Address: '+ formattedAddress + '\n' + 'Lat: '+ lat + '\n' + 'Lng: '+ lng);

                  }

              }

          });
    }

注意:lat = 位置.lat; & lng = 位置.lng;而不是 lat = location.lat(); & lng = 位置.lng();

关于javascript - Google 地理编码 v3 - 获取格式化地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16505803/

相关文章:

javascript - imacros javascript 将数据保存到文件,在 Firefox 中给出错误

javascript - 无法在 Fabric.js 中使用 getActiveObject()

javascript - 如何使用不同的 div ID 调用相同的函数?

google-maps - MarkerClusterer 完成后触发事件

google-maps - 如何在 HTML5 中的 Google map 上显示用户的位置?

javascript - 我想跳过对象中的最后一个属性并将其值分配给上一个属性

javascript - Meteor/Iron Router 外部重定向

javascript - react 组件没有出现

jquery - 绝对居中 div 的优雅方式?

google-maps - 来自域名的 ZK 应用程序中用于 Google map 的 Google API key