简体   繁体   中英

How to restrict Google places api autocomplete for some city names and all state names?

<form>
    <input id="pac-input" value="" class="controls" type="text" placeholder="Search Box">
    <input type="button" id="find" value="find" />
</form>

<script>
function initAutocomplete() {

      var options = {
          componentRestrictions: { country: "IN" }
      };
      // Create the search box and link it to the UI element.

      var input = document.getElementById('pac-input');
      var searchBox = new google.maps.places.Autocomplete(input, options);
}
</script>

<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD9CiSrNomU7HPw5PLIpFZYP6NXXOq0dlE&libraries=places&callback=initAutocomplete"
            async defer></script>

I want that in searchbox text like "Maharashtra,india", "mumbai", "india" should not come.Basically I need only regions and localities and not state names or city names to appear in suggestion. Can anybody help ?

Please refer : https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete

If you look at the sample code in the 'JavaScript + HTML' section, the API allows you to set filters, for example, establishments. So, just set the types to your criteria and you should be good to go.

Cheers, Mrugesh

How to limit google autocomplete results to City and Country only

You can refer this answer.

 function initialize() { var options = { types: ['(regions)'], componentRestrictions: {country: "us"} }; var input = document.getElementById('searchTextField'); var autocomplete = new google.maps.places.Autocomplete(input, options); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM