使用自动完成服务()将 Google 地图地点限制为特定国家/地区
Restricting Google Maps Places to Specific Countries using AutocompleteService()
使用google.maps.places.AutocompleteService
如何限制搜索国家(加拿大和美国)。 如果您使用的是AutoComplete
则可以将国家/地区添加到选项中,但使用AutocompleteService
似乎并非如此。 我在查看文档时也发现了componentRestrictions
,但这也不起作用。
您如何限制使用 Google 地方信息搜索的国家/地区?
function initService(controlInput) {
var service = new google.maps.places.AutocompleteService();
var displaySuggestions = function(predictions, status) {
if (status != google.maps.places.PlacesServiceStatus.OK) {
console.log(status);
return;
}
predictions.forEach(function(prediction) {
console.log(prediction);
});
};
controlInput.addEventListener('keyup', function(event, input) {
service.getQueryPredictions({
input: controlInput.value,
//country: 'ca', <--- doesn't work
componentRestrictions: {
country: 'ca' <--- also doesn't work
}
}, displaySuggestions);
});
}
尝试遵循组件筛选中的本指南:
在地理编码响应中,Google 地图地理编码 API 可以返回仅限于特定区域的地址结果。限制是使用组件过滤器指定的。筛选器由由竖线 (|) 分隔的组件:值对列表组成。仅返回与所有筛选器匹配的结果。过滤器值支持与其他地理编码请求相同的拼写更正和部分匹配方法。如果地理编码结果与组件过滤器部分匹配,则响应中将包含partial_match字段。
可以筛选的组件包括:
路由
- 匹配路由的长名称或短名称。
局部性- 与局部性和子局部性类型匹配。
- administrative_area匹配所有administrative_area级别。
- postal_code匹配postal_code和postal_code_prefix。
国家- /地区与国家/地区名称或两个字母的 ISO 3166-1 国家/地区代码匹配。
注 : 每个地址组件只能在地址参数中指定或作为组件过滤器指定,而不能同时指定两者。这样做可能会导致
ZERO_RESULTS
。带有
components=country:ES
的"圣克鲁斯"的地理编码将返回西班牙加那利群岛的圣克鲁斯德特内里费岛。请求:
https://maps.googleapis.com/maps/api/geocode/json?address=santa+cruz&components=country:ES&key=YOUR_API_KEY
同样在这里 将搜索限制在特定国家/地区,了解如何实施限制搜索并遵循ISO 3166-1(两个字母的国家/地区代码)。我希望这有所帮助。
您正在使用不接受组件限制选项的 AutocompleteService.getQueryPredictions 方法。它对 AutocompleteService.getPlacePredictions 方法有效。请参阅文档:
自动完成服务:https://developers.google.com/maps/documentation/javascript/reference#AutocompleteService
自动完成请求:https://developers.google.com/maps/documentation/javascript/reference#AutocompletionRequest
查询自动完成请求:https://developers.google.com/maps/documentation/javascript/reference#QueryAutocompletionRequest
我使用了一个国家/地区选择选项来设置autocomplete.setComponentLimits的值。
自动完成将仅限于从该国家/地区进行搜索。请原谅格式,因为我刚刚将其转储在这里。
function setAutocompleteCountry() {
var country = document.getElementById('id_countryselect').value;
if (country === 'ALL') {
autocomplete.setComponentRestrictions({'country': []});
} else {
autocomplete.setComponentRestrictions({'country': country});
}
}
autocomplete.addListener('place_changed', function () {
var place = autocomplete.getPlace();
------> Rest of code
您可以在启动自动完成服务后将其与 getPlacesPredicitions 一起使用。您可以像这样将其包含在请求对象中:
const service = new google.maps.places.AutocompleteService();
let request = {
input: val,
componentRestrictions: {country: 'us'},
};
service.getPlacePredictions(request, (predictions, status) => {
this.addresses = predictions
//each predicition is one address
});
- Possibile使用Manifest v2下的HTML5地理位置API将lat/long转换为国家/地区
- 无法使用国家/地区选择中的状态动态填充下拉列表
- 在Chrome扩展中,我如何准确地检索用户'的地区/地区/国家
- 使用自动完成服务()将 Google 地图地点限制为特定国家/地区
- Magento:根据所选国家/地区隐藏/显示输入字段
- 仅在引导程序表单助手国家/地区选取器中显示国家/地区子集文本
- 如何从特定国家/地区重定向用户
- 根据国家/地区更改货币
- 如何将 gmap 搜索自动完成功能限制为有限的国家/地区
- 带有标志和文本的国家/地区建议列表
- 选择国家/地区时更改颜色
- 相对于选定的地区中心缩放SVG国家/地区地图
- Three.js地球仪-使国家/地区可点击
- 传递国家名称时获取所有州/地区/城市
- javascript从所选国家/地区值中选择城市
- 如何获得用户'的国家/地区,并转发到URL
- 下拉菜单,用于选择国家/地区,然后选择州
- 如果在JavaScript w/HTML中选择了特定国家/地区,如何要求邮政编码为数字
- 我如何限制谷歌地图API's的搜索框将搜索到一个国家/地区
- 以JSON格式Shopify地区(国家)