我正在使用谷歌地方api自动完成位置作为用户类型.但是,我如何将建议限制在特定国家/地区?
这是我的javascript
function initAutocomplete() { // Create the search Box and link it to the UI element. var input = document.getElementById('autocomplete-input'); var searchBox = new google.maps.places.SearchBox(input); // [START region_getplaces] // Listen for the event fired when the user selects a prediction and retrieve // more details for that place. searchBox.addListener('places_changed',function() { var places = searchBox.getPlaces(); if (places.length == 0) { return; } }); }
解决方法
通过将SearchBox更改为自动完成并传递带有要求的选项来修复它.如果有人需要,这是代码.
function initAutocomplete() { // Create the search Box and link it to the UI element. var input = document.getElementById('autocomplete-input'); var options = { componentRestrictions: {country: 'fr'} }; var searchBox = new google.maps.places.Autocomplete(input,options); // [START region_getplaces] // Listen for the event fired when the user selects a prediction and retrieve // more details for that place. searchBox.addListener('places_changed',function() { var places = searchBox.getPlaces(); if (places.length == 0) { return; } }); }