EnexoOnoma
EnexoOnoma

Reputation: 8834

Google maps autocomplete to restrict places shown in a specific country?

I am using google maps autocomplete correctly, however I want to restrict all the places shown to a specific country.

What modifications shall I do into the code in order to achieve that?

  function initAutocomplete() {
    autocomplete = new google.maps.places.Autocomplete(
        (document.getElementById('autocomplete')),
        {types: ['geocode']});
  }


  function geolocate() {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function(position) {
        var geolocation = {
          lat: position.coords.latitude,
          lng: position.coords.longitude
        };

        var circle = new google.maps.Circle({
          center: geolocation,
          radius: position.coords.accuracy
        });
        autocomplete.setBounds(circle.getBounds());
      })

;
        }
      }

Upvotes: 2

Views: 7799

Answers (2)

Gaurav Mithas
Gaurav Mithas

Reputation: 75

just try adding it after '{types:[geocode]}'

      autocomplete = new google.maps.places.Autocomplete(
    (document.getElementById('autocomplete')),
    {types: ['geocode'], componentRestrictions: {country: 'fr'}});

Upvotes: 0

Atul Jindal
Atul Jindal

Reputation: 976

Use the componentRestrictions option to restrict the autocomplete search to a particular country. The following code restricts the results to cities within France.

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

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

Upvotes: 10

Related Questions