谷歌地理编码 - 坐标地址与坐标
问题描述:
所以这就是我所追求的,我被告知它不可能,但我不会放弃!谷歌地理编码 - 坐标地址与坐标
比方说,在“伦敦”将用户类型分为我的位置搜索框,点击“地理编码”我能得到该位置的坐标有点像这个例子:
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);
}
}
});
}
编辑:
为什么不可能,Google地图会返回您之后的所有数据,但请注意,使用Google地理编码还需要您使用可见地图。 – adeneo 2013-05-11 12:36:26