如何获得自动完成地图的结果只有一个国家? [英] How to get results of autocomplete maps only one country?
问题描述
如何获取自动填充地图的结果只有一个国家/地区?
你没有指定你正在使用的API。根据 Google Places API > docs 有一个可选的组件
参数。来自文档:
组件 - 您想要限制
结果的一组地方。目前,您可以使用组件按国家进行过滤。
该国家必须以ISO 3166-1 Alpha-2
兼容国家代码的双字符形式通过。例如:components = country:fr would
将结果限制在法国境内。
这意味着您只需添加& components = country:fr
到您的网址并自动完成限于法国。
$ b 如果您使用
Google maps Js API 的 PlacesService
您可以使用 componentsRestriction
参数。有一个国家
属性,根据文件:
限制预测指定的国家(ISO 3166-1 Alpha-2
国家代码,不区分大小写)。例如,us,br,au。
编辑
如果你还没有选择API,那么这里有一个简单的例子(注意这行: country:'us'
,你将不得不输入你想要的国家代码列表此处。需要小写!):
在此处输入位置:< input id =searchTextFieldtype = textsize =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>
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.
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>
这篇关于如何获得自动完成地图的结果只有一个国家?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!