Google 地理编码 - 获取地址和坐标

2023-12-02

这就是我所追求的,有人告诉我这是不可能的,但我还不会放弃!

假设用户在我的位置搜索框中输入“伦敦”并单击“地理编码”,我可以获得该位置的坐标,类似于此示例:

http://gmaps-samples-v3.googlecode.com/svn/trunk/geocoder/v3-geocoder-tool.html

但假设我有以下字段:

Town:
City:
Country:
Long:
Lat:

是否可以通过此请求填写所有这些字段,而不仅仅是坐标?例如,我可以将以下信息存储在 cookie 中:

Town: -
City: London
Country: UK
Long: 123.45
Lat: 123.45

尝试这个:

function getLatLongDetail(latLng) {

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

                  var address = "", city = "", state = "", zip = "", country = "";


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


                  alert('City: '+ city + '\n' + 'State: '+ state + '\n' + 'Zip: '+ zip + '\n' + 'Country: ' + country);

              }

          }

      });
}

EDIT:

示例示例在这里.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Google 地理编码 - 获取地址和坐标 的相关文章

随机推荐