javascript Google Geocoding v3 - 获取格式化地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16505803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:57:00  来源:igfitidea点击:

Google Geocoding v3 - Getting formatted Address

javascriptjquerygoogle-mapsgoogle-maps-api-3geolocation

提问by Jimmy

I was wondering if anyone is able to help me. I am having a hard time getting both the coordinates and the formatted address returned to me in my dialogue box on my geocoding result, even though I seem to be able to grab the city, country.

我想知道是否有人能够帮助我。我很难在地理编码结果的对话框中获得坐标和格式化地址返回给我,即使我似乎能够抓住城市,国家。

Here is the code I am working with:

这是我正在使用的代码:

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

Code

代码

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;
 };

Also, since I am new to Javascript I am open to any ideas about writing this more cleanly.

另外,由于我是 Javascript 的新手,我对任何关于更干净地编写它的想法持开放态度。

回答by Yaqub Ahmad

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);

                  }

              }

          });
    }

Note: lat = location.lat; & lng = location.lng; instead of lat = location.lat(); & lng = location.lng();

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