我正确使用谷歌地图自动填充,但我想限制显示到特定国家/地区的所有地点。
为了达到这个目的,我应该对代码做些什么修改?
function initAutocomplete() {
autocomplete = new google.maps.places.Autocomplete(
(document.getElementById('autocomplete')),
{types: ['geocode']});
}
function geolocate() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var geolocation = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
var circle = new google.maps.Circle({
center: geolocation,
radius: position.coords.accuracy
});
autocomplete.setBounds(circle.getBounds());
})
;
}
}
答案 0 :(得分:7)
使用componentRestrictions选项将自动填充搜索限制为特定国家/地区。以下代码将结果限制在法国境内的城市。
var input = document.getElementById('searchTextField');
var options = {
types: ['(cities)'],
componentRestrictions: {country: 'fr'}
};
autocomplete = new google.maps.places.Autocomplete(input, options);
答案 1 :(得分:0)
尝试在'{types:[geocode]}'
之后添加它 autocomplete = new google.maps.places.Autocomplete(
(document.getElementById('autocomplete')),
{types: ['geocode'], componentRestrictions: {country: 'fr'}});