Google地理编码 - 抓取地址和坐标 [英] Google Geocoding - Grab Address along with coordinates

查看:160
本文介绍了Google地理编码 - 抓取地址和坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



让我们说用户在我的位置搜索框中输入London并点击Geocode我可以获得该位置的坐标,如下所示:



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



但是可以说我拥有以下字段:

 城镇:
城市:
国家:
长:
纬度:

这个请求是否可以填充所有这些字段,而不仅仅是坐标?例如,我将获得以下信息,然后可以将其存储在cookie中:

 城镇: -  
城市:伦敦
国家:英国
长:123.45
纬度:123.45




 函数getLatLongDetail(latLng){

var geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng':latLng},
函数(结果,状态){
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];
// if如果(addr.types [0] =='country')
country = addr.long_name;
else if(addr.types [0]),则address_components中的此条目具有一个国家类型
。 =='street_address')// address 1
address = address + addr.long_name;
else if(addr.types [0] =='establishment')
address = address + addr .long_name;
els (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 +'\\\
'+'State:'+ state +'\\\
'+'Zip:'+邮编+'\\\
'+'国家:'+国家);

}

}

});

编辑:

示例示例在这里


So this is what I am after, and I've been told it isn't possibly but I am not going to give up just yet!

Let's say the user types in "London" into my location search box and clicks "Geocode" I am able to get the co-ordinates of that location a bit like this example:

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

But lets say that I have the following fields:

Town:
City:
Country:
Long:
Lat:

Is it possible to have all these fields filled in by this request, not just the co-ordinates? For example, I would have the following information which I could then store in a cookie:

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

解决方案

try this:

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:

Sample example is here.

这篇关于Google地理编码 - 抓取地址和坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆