Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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());
      })

;
        }
      }
like image 657
EnexoOnoma Avatar asked Jan 24 '26 16:01

EnexoOnoma


1 Answers

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);
like image 162
Atul Jindal Avatar answered Jan 27 '26 07:01

Atul Jindal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!