从反向地理编码中获取完整格式的地址 [英] Get the full formatted address from Reverse Geocoding
问题描述
我想通过Google地图API v3反向地理编码检索完整格式的地址,因为它只在许多(可能是所有)国家/地区显示邮政编码和/或城市和国家/地区。我还希望通过反向地理编码(地址查询)获取街道名称和其他内容, a>功能。但是由于我无法从XML文件或通过JavaScript链接到XML文件的地址获取数据,因此我必须使用
I want to retrieve the full formatted address via Google Maps API v3 Reverse Geocoding, since it only shows the postal code and/or city and country in many (maybe all) countries. I want to also retrieve the street name and the other stuff you can get through the Reverse Geocoding (Address Lookup) function. But since I can't get data from XML files or addresses that are linked to an XML file in JavaScript, I must use the Reverse Geocoding function.
反向地理编码(地址查找)显示 791 Long Ridge Lane,Gainesboro,田纳西州38562 ,美国
和反向地理编码显示 Gainesboro,田纳西州38562,美国
。
Reverse Geocoding (Address Lookup) shows 791 Long Ridge Lane, Gainesboro, Tennessee 38562, USA
and Reverse Geocoding shows Gainesboro, Tennessee 38562, USA
.
甚至可以通过反向地理编码获得完整格式的地址,或者是否必须通过反向地理编码(地址查找)来使用PHP XML文件?
Is it possible to get the full formatted address even through Reverse Geocoding, or do I have to use the PHP XML file through Reverse Geocoding (Address Lookup)?
function coordinates_to_address(lat, lng) {
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
if(results[1]) {
$('#address_current').text(results[1].formatted_address);
} else {
alert('No results found');
}
} else {
var error = {
'ZERO_RESULTS': 'Kunde inte hitta adress'
}
// alert('Geocoder failed due to: ' + status);
$('#address_new').html('<span class="color-red">' + error[status] + '</span>');
}
});
}
推荐答案
为了获得最精确的结果,使用第一个结果( results [0]
),而不是第二个结果( results [1]
): p>
To get the most precise result, use the first result (results[0]
), not the second (results[1]
):
function coordinates_to_address(lat, lng) {
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
if(results[0]) {
$('#address_current').text(results[0].formatted_address);
} else {
alert('No results found');
}
} else {
var error = {
'ZERO_RESULTS': 'Kunde inte hitta adress'
}
// alert('Geocoder failed due to: ' + status);
$('#address_new').html('<span class="color-red">' + error[status] + '</span>');
}
});
}
这篇关于从反向地理编码中获取完整格式的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!