嗨,我有一个代码,只搜索澳大利亚的城市。
我怎样才能搜索邮政编码?
请参阅我的代码:
function initialize() {
var ac = new google.maps.places.Autocomplete(
(document.getElementById('autocomplete')), {
types: ['(cities)'],
componentRestrictions: {
country: 'AU'
}
});
ac.addListener('place_changed', function() {
var place = ac.getPlace();
if (!place.geometry) {
// User entered the name of a Place that was not suggested and
// pressed the Enter key, or the Place Details request failed.
window.alert("No details available for input: '" + place.name + "'");
return;
}
// Alert the selected place
window.alert("You selected: '" + place.formatted_address + "'");
});
}
#autocomplete {
width: 300px;
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=places&callback=initialize" async defer></script>
<input id="autocomplete" placeholder="Enter your address" type="text"></input>
在类型中使用regions
而不是cities
,导致cities
仅限于locality
或administrative_area_level_3
,其中regions
包括postal_code
- 地区
- 次区
- 国家
- administrative_area_level_1
- administrative_area_level_2
检查来源 : (https://developers.google.com/places/web-service/autocomplete#place_types(
使用以下更新的代码
`var ac = new google.maps.places.Autocomplete(
(document.getElementById('autocomplete')), {
types: ['(regions)'],
componentRestrictions: {
country: 'AU'
}
});`
还要检查更新的代码片段。
function initialize() {
var ac = new google.maps.places.Autocomplete(
(document.getElementById('autocomplete')), {
types: ['(regions)'],
componentRestrictions: {
country: 'AU'
}
});
ac.addListener('place_changed', function() {
var place = ac.getPlace();
if (!place.geometry) {
// User entered the name of a Place that was not suggested and
// pressed the Enter key, or the Place Details request failed.
window.alert("No details available for input: '" + place.name + "'");
return;
}
// Alert the selected place
window.alert("You selected: '" + place.formatted_address + "'");
});
}
#autocomplete {
width: 300px;
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=places&callback=initialize" async defer></script>
<input id="autocomplete" placeholder="Enter your address" type="text"></input>