admin管理员组文章数量:1330427
Following the guide found I am trying to filter my search box to only show suggestions for the UK.
I have the code:
<script src=";libraries=places&callback=initialize&v=3"
async defer></script>
<script>
function initialize() {
setTimeout(function() {
initMap();
initAutoplete();
}, 4000);
}
function initAutoplete() {
var options = {
types: [],
ponentRestrictions: {country: 'GB' }
};
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input, options);
}
</script>
Which works to the point that the search box gets initialised and is usable however its not filtering to just UK locations. The box will show locations remendations for the world over.
Using GB or gb gives the same result
Following the guide found https://developers.google./maps/documentation/javascript/places-autoplete#places_searchbox I am trying to filter my search box to only show suggestions for the UK.
I have the code:
<script src="https://maps.googleapis./maps/api/js?key=XXXXXXX&libraries=places&callback=initialize&v=3"
async defer></script>
<script>
function initialize() {
setTimeout(function() {
initMap();
initAutoplete();
}, 4000);
}
function initAutoplete() {
var options = {
types: [],
ponentRestrictions: {country: 'GB' }
};
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input, options);
}
</script>
Which works to the point that the search box gets initialised and is usable however its not filtering to just UK locations. The box will show locations remendations for the world over.
Using GB or gb gives the same result
Share edited Jul 16, 2018 at 18:20 DevWithZachary asked Jul 16, 2018 at 18:12 DevWithZacharyDevWithZachary 3,68511 gold badges52 silver badges106 bronze badges 2-
Their example (developers.google./maps/documentation/javascript/…) says lowercase country:
ponentRestrictions: {country: 'fr'}
. Try lowercasing it? – xrd Commented Jul 16, 2018 at 18:16 - Can we filter by City addresses? – Sangwin Gawande Commented Sep 21, 2022 at 14:22
4 Answers
Reset to default 4In the end switching to
function initAutoplete() {
var input = document.getElementById('pac-input');
var options = {
types: ['(cities)'],
ponentRestrictions: {country: 'GB'}
};
var autoplete = new google.maps.places.Autoplete(input, options);
}
fixed my issue
Maybe try setComponentRestrictions
method.
var options = {
types: []
};
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input, options);
// This method.
searchBox.setComponentRestrictions({'country': ['gb']});
Try with the country lowercased:
<script src="https://maps.googleapis./maps/api/js?key=XXXXXXX&libraries=places&callback=initialize&v=3"
async defer></script>
<script>
document.onload = function initialize() {
initMap();
initAutoplete();
}
function initAutoplete() {
var options = {
types: [],
ponentRestrictions: {country: 'gb' }
};
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input, options);
}
</script>
If using api directly add &ponents=country:uk
e.g: https://developers.google./maps/documentation/places/web-service/autoplete
本文标签: javascriptFilter Google Places API for only one countryStack Overflow
版权声明:本文标题:javascript - Filter Google Places API for only one country - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742262029a2442690.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论