我正在尝试在googlemaps视口中心发生更改时,使用邮政编码向我的数据库提交查询。我知道可以使用类似以下内容的反向地理编码来完成此操作:
google.maps.event.addListener(map, 'center_changed', function(){
newCenter();
});
...
function newCenter(){
var newc = map.getCenter();
geocoder.geocode({'latLng': newc}, function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
var newzip = results[0].address_components['postal_code'];
}
});
};
当然,这段代码实际上是不起作用的。所以我想知道我需要如何更改它才能从结果数组中提取邮政编码。
types
数组中具有“postal_code”的元素,该元素可能位于最后、最前或任何位置。此外,哪个元素?您可能希望从results[0].address_components
或results
本身获取邮政编码:尝试两者并查看在您关心的区域中哪个更好用。一般来说,如果您关心具有完整地址的地点,则建议使用results[0].address_components
,如果您关心严格包含您的latlng的邮政编码,则使用results
。 - miguev