Autocomplete Autocomplete in Google Places Australia only

How to limit Google Places to Australia only?

Here is the code I'm using:

function initialize() {

  var input = document.getElementById('searchTextField');
  var autocomplete = new google.maps.places.Autocomplete(input);
}

google.maps.event.addDomListener(window, 'load', initialize);

      

+3


source to share


1 answer


In the documentation, autocomplete :

Limit search to a specific country

Use the componentRestrictions parameter to restrict the autocomplete search to a specific country. The following code limits the results to cities in France.

var input = document.getElementById('searchTextField');
var options = {
  types: ['(cities)'],
  componentRestrictions: {country: 'fr'}
};

autocomplete = new google.maps.places.Autocomplete(input, options);

      

For Australia use "au":



function initialize() {
  var options = {
    componentRestrictions: {country: 'au'}
  };

  var input = document.getElementById('searchTextField');
  var autocomplete = new google.maps.places.Autocomplete(input, options);
}

google.maps.event.addDomListener(window, 'load', initialize);

      

snippet of code:

function initialize() {
  var options = {
    componentRestrictions: {country: 'au'}
  };

  var input = document.getElementById('searchTextField');
  var autocomplete = new google.maps.places.Autocomplete(input, options);
}

google.maps.event.addDomListener(window, 'load', initialize);
      

<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
<input id="searchTextField" type="text" />
      

Run codeHide result


+6


source







All Articles