将自动填充列表中的国家/地区的Google地图限制为“INDIA,USA和UK” [英] limit Google maps of countries in the autocomplete list to "INDIA, USA and UK"
问题描述
此代码无法使用。请告诉我确切的解决方案
This code is not working. Please tell me the exact solution
<script src="maps.googleapis.com/maps/api/…; type="text/javascript"></script>
<script type="text/javascript">
function initialize() {
var input = document.getElementById('searchTextField');
/* restrict to multiple cities? */
var options = {
types: ['(cities)'],
componentRestrictions: {country: ["usa", "uk"]}
};
var autocomplete = new google.maps.places.Autocomplete(input, options);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
更新
2017年1月,Google地图JavaScript API 3.27版中引入了多个国家/地区过滤器自动完成功能:
Multiple countries filter in place autocomplete was introduced in version 3.27 of Maps JavaScript API in January 2017:
您现在可以将自动填充预测限制为仅来自多个国家/地区。 ou可以通过在AutocompleteOptions的componentRestrictions字段中指定多达5个国家/地区来完成此操作。
You can now restrict Autocomplete predictions to only surface from multiple countries. You can do this by specifying up to 5 countries in the componentRestrictions field of the AutocompleteOptions.
https://developers.google.com/maps/documentation/javascript/releases#327
推荐答案
首先,您不能从多个国家过滤。这是在此处报告的已知问题。
First of all, you cannot filter from multiple countries. It's a known issue reported here.
然后对于您的代码,您必须为国家/地区代码使用两个字符的字符串:
Then for your code, you have to use a two characters string for the country code :
componentRestrictions可以用来限制结果到特定的
组。目前,您可以使用componentRestrictions按
国家进行过滤。该国必须通过两个字符,ISO 3166-1
Alpha-2兼容国家代码。 来源。
以下是一个国家/地区的工作示例我非常喜欢(取自 Google样本列表)。
Here is a one-country working sample I jsfiddled (taken from the Google samples list).
这篇关于将自动填充列表中的国家/地区的Google地图限制为“INDIA,USA和UK”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!