javascript 如何使用谷歌地图反转地理编码坐标以获取邮政密码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16700035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reverse geocode coordinates to get postal pincode using google map?
提问by ThinkFloyd
I am making an app which will take user's current location or his customized map marker to find out lat & long and then using those values I would like to know pincode(zipcode) of that area so that I can tell user whether goods can be delivered in that area or not.
我正在制作一个应用程序,它将获取用户的当前位置或他的自定义地图标记来找出经纬度,然后使用这些值我想知道该区域的密码(邮政编码),以便我可以告诉用户是否可以交付货物在那个地区与否。
I have tried this : http://www.geonames.org/export/ws-overview.htmlbut it doesn't have full data and whatever it has is not very accurate. Is there any other API which I can use to get such data?
我试过这个:http: //www.geonames.org/export/ws-overview.html但它没有完整的数据,而且它所拥有的一切都不是很准确。我可以使用其他任何 API 来获取此类数据吗?
回答by geocodezip
If you have a location (and a Google Maps API v3 map), reverse geocodethe location. Process through the returned records for the postal_code (see this SO post for an example).
如果您有位置(和 Google Maps API v3 地图),请对该位置进行反向地理编码。处理 postal_code 的返回记录(有关示例,请参阅此 SO 帖子)。
// assumes comma separated coordinates in a input element
function codeLatLng() {
var input = document.getElementById('latlng').value;
var latlngStr = input.split(',', 2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, processRevGeocode);
}
// process the results
function processRevGeocode(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var result;
if (results.length > 1)
result = results[1];
else
result = results[0];
if (result.geometry.viewport)
map.fitBounds(result.geometry.viewport);
else if (result.geometry.bounds)
map.fitBounds(result.geometry.bounds);
else {
map.setCenter(result.geometry.location);
map.setZoom(11);
}
if (marker && marker.setMap) marker.setMap(null);
marker = new google.maps.Marker({
position: result.geometry.location,
map: map
});
infowindow.setContent(results[1].formatted_address);
infowindow.open(map, marker);
displayPostcode(results[0].address_components);
} else {
alert('Geocoder failed due to: ' + status);
}
}
// displays the resulting post code in a div
function displayPostcode(address) {
for (p = address.length-1; p >= 0; p--) {
if (address[p].types.indexOf("postal_code") != -1) {
document.getElementById('postcode').innerHTML= address[p].long_name;
}
}
}