问题
I need to get the autocomplete values based on some city using google place API in Javascript. But as per my code its not working as expected.
Here I have some cities in array like city=['Bhubaneswar','Balesore','abcd'] and When user will type to get the place the matching should be belongs to these cities only. I am explaining my code below.
<script src="https://maps.googleapis.com/maps/api/js?key=MAP_KEY&libraries=places&callback=initMap" type="text/javascript" async defer></script>
function initMap() {
var city=['Bhubaneswar','Balesore','abcd'];
var input = document.getElementById('pick_addr');
var options = {
types: ['(cities)'],
componentRestrictions: {country: "IN"}
};
var autocomplete = new google.maps.places.Autocomplete(input,options);
}
As per my code its coming for one country but I need only per city based.
回答1:
It is not currently possible to restrict Places Autocomplete results to specific cities/localities. As a workaround for now, you can bias the autocomplete results to favor an approximate location or area as stated in this documentation.
There is also an existing feature request to filter using route, locality, administrative_area, postal_code and country in the Places API autocomplete. I'd recommend you "star" it to receive updates. https://issuetracker.google.com/issues/35822067
Hope this helps!
来源:https://stackoverflow.com/questions/56897475/how-to-get-the-autocomplete-list-based-on-array-of-cities-using-javascript