Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

jquery - How to get results of autocomplete maps only one country?

How to get results of autocomplete maps only one country?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...