Google Maps Geocoder:返回postal_code

我正在使用Jquery UI使用Google Maps Geocoder自动完成对地址的搜索,当选择地址时,它会返回地理编码信息。 这是片段:

$('#address-dropdown').autocomplete({ //Use geocoder to fetch lat+long values from an address input using google maps API source: function(request, response) { geocoder.geocode( {'address': request.term }, function(results, status) { response($.map(results, function(item) { return { label: item.formatted_address, value: item.formatted_address, location: item.geometry.location, latitude: item.geometry.location.lat(), longitude: item.geometry.location.lng() } })); }) }, 

我需要能够返回用途“postal_code”来检查它们是否存在于允许的区域内。 以下是一个示例输出: http : //maps.googleapis.com/maps/api/geocode/json?address = 1601 + Main + St +Eaton+Rapids + MI +48864&_sensor = false

我如何定位postal_code信息?

看看我的例子: http : //jsfiddle.net/nEKMK/

o是你的对象。

 if (o.results[0].address_components) { for (var i in o.results[0].address_components) { if (typeof(o.results[0].address_components[i]) === "object" && o.results[0].address_components[i].types[0] == "postal_code") { var result = o.results[0].address_components[i].long_name; } } } if (!result) { alert("Error, there is no postal code"); } else { alert(result); }