Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How to get results of autocomplete maps only one country?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
177 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

EDIT

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='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...