【问题标题】:Google Geocoding - Grab Address along with coordinates谷歌地理编码 - 获取地址和坐标
【发布时间】:2013-05-06 00:18:21
【问题描述】:

所以这就是我所追求的,我被告知这是不可能的,但我还不会放弃!

假设用户在我的位置搜索框中输入“伦敦”并单击“地理编码”,我能够获得该位置的坐标,有点像这个例子:

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

【问题讨论】:

  • 为什么不可能,Google 地图会返回您所追求的所有数据,但请注意,使用 Google 的地理编码还需要您使用可见地图。

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


【解决方案1】:

试试这个:

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

              }

          }

      });
}

编辑:

Sample example is here.

【讨论】:

  • 您好 Yaqub,非常感谢您的回复。我已将此代码放入 jfiddle 但我对如何实现它感到困惑,因为我是 javascript 新手。这就是我在资源中包含您的代码和谷歌地图 API 的程度。你能指点我的方向,以便我看看它是如何工作的吗? jsfiddle.net/QA7Xr
猜你喜欢
  • 1970-01-01
  • 2020-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多