2

I am using google.maps.places.Autocomplete, I want city to autocomplete but restrict the autocomplete results to multiple cities, not just one as in the google documentation.

this is what I have

<script src="http://maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places" type="text/javascript"></script>
<script type="text/javascript">
 function initialize() {

    var input = document.getElementById('searchTextField');

    /* restrict to multiple cities? */
    var options = {
       types: ['(cities)'],
       componentRestrictions: {country: "us", "fr"}
    };


    var autocomplete = new google.maps.places.Autocomplete(input, options);
 }
 google.maps.event.addDomListener(window, 'load', initialize);
</script>

<div>
<input id="searchTextField" type="text" size="50" placeholder="Enter a location" autocomplete="on">
</div>
Francois
  • 10,465
  • 4
  • 53
  • 64
  • Ironically, this is the first example that I have found for filtering by country. Thanks! Where did you go for documentation btw? – fatfrog Nov 30 '14 at 22:06
  • Workaround here: http://stackoverflow.com/questions/11290755/limit-google-maps-of-countries-in-the-autocomplete-list-to-india-usa-and-uk/36064059#36064059 – metamagikum Mar 17 '16 at 14:55

3 Answers3

3

Actually the Places API supports only 1 country as restriction. ComponentRestrictions

sgimeno
  • 1,883
  • 1
  • 22
  • 35
1

I am new to google places autocomplete but I think the error is here:

componentRestrictions: {country: "us", "fr"}

Try this:

componentRestrictions: {country: ["us", "fr"]}
Naor
  • 23,465
  • 48
  • 152
  • 268
0

You can only add 1 country for restrictions. https://developers.google.com/maps/documentation/javascript/reference#ComponentRestrictions

prestarocket
  • 733
  • 3
  • 12
  • 30