huon sokly huon sokly - 5 months ago 47
Javascript Question

How to get results of autocomplete maps only one country?

How to get results of autocomplete maps only one country?


You didn't specify which API you are using. Using Google Places API, according to the docs there is an optional components parameter. From docs:

components — A grouping of places to which you would like to restrict your results. Currently, you can use components to filter by country. The country must be passed as a two character, ISO 3166-1 Alpha-2 compatible country code. For example: components=country:fr would restrict your results to places within France.

That means you will just add &components=country:fr to your url and autocomplete is restricted to france.

If you use Google maps Js API's PlacesService you can restrict the results of google maps places autocomplete using componentsRestriction parameter. There is a country attribute, which according to the docu:

Restricts predictions to the specified country (ISO 3166-1 Alpha-2 country code, case insensitive). E.g., us, br, au.


If you didn't choose API yet, here is a simple example (Notice this line: country: 'us' , you will have to input your desired country code there. List of codes here. Needs to be lower case!):

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

$(function() {
  // Initialize Autocomplete
  var options = {
    componentRestrictions: {
      country: 'us' //PUT YOUR COUNTRY CODE HERE!!
  var autocomplete = new google.maps.places.Autocomplete(pac_input, options);
Input location here:<input id="searchTextField" type="text" size="50">
<script src=''></script>
<script src=""></script>