[英]How to Search Post Code and Cities Google Maps
Hi I have a code that searching only the cities on Australia.嗨,我有一个只搜索澳大利亚城市的代码。
How can I able to search the Post Code also?我怎样才能搜索邮政编码?
Please see my code:请看我的代码:
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>
use regions
instead cities
in types cause cities
limited to locality
or administrative_area_level_3
where regions
includes postal_code
along with在类型中使用
regions
而不是cities
会导致cities
仅限于locality
或administrative_area_level_3
regions
postal_code
,其中regions
包括postal_code
以及
check source : ( https://developers.google.com/places/web-service/autocomplete#place_types )检查来源:( https://developers.google.com/places/web-service/autocomplete#place_types )
use below updated code使用下面的更新代码
`var ac = new google.maps.places.Autocomplete(
(document.getElementById('autocomplete')), {
types: ['(regions)'],
componentRestrictions: {
country: 'AU'
}
});`
also check updated code snippet.还要检查更新的代码片段。
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.