2013-05-11 125 views
2

所以这就是我所追求的,我被告知它不可能,但我不会放弃!谷歌地理编码 - 坐标地址与坐标

比方说,在“伦敦”将用户类型分为我的位置搜索框,点击“地理编码”我能得到该位置的坐标有点像这个例子:

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 
+0

为什么不可能,Google地图会返回您之后的所有数据,但请注意,使用Google地理编码还需要您使用可见地图。 – adeneo 2013-05-11 12:36:26

回答

6

试试这个:

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

+0

您好Yaqub,非常感谢您的回复。我已经把这段代码放入jfiddle中,但我对如何实现它感到困惑,因为我是JavaScript新手。这是我得到了多少,以将您的代码和谷歌地图API包含在资源中。你能指出我的方向,所以我可以看到它的工作原理吗? http://jsfiddle.net/QA7Xr/ – Jimmy 2013-05-11 14:30:32

+1

http://jsfiddle.net/QA7Xr/2/ – 2013-05-11 15:44:16

+0

非常感谢你 – Jimmy 2013-05-11 16:57:39