jquery - 将自动完成列表中的国家/地区 Google map 限制为 "INDIA, USA and UK"

标签 jquery google-maps

此代码不起作用。请告诉我具体的解决办法

<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 月的 Maps JavaScript API 3.27 版中引入了多个国家/地区过滤器自动完成功能:

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

最佳答案

首先,您无法从多个国家/地区进行过滤。这是一个已知问题 reported here .

然后对于您的代码,您必须使用两个字符的字符串作为国家/地区代码:

componentRestrictions can be used to restrict results to specific groups. Currently, you can use componentRestrictions to filter by country. The country must be passed as as a two character, ISO 3166-1 Alpha-2 compatible country code. Source.

这是一个国家working sample我摆弄了(取自 Google samples list )。

关于jquery - 将自动完成列表中的国家/地区 Google map 限制为 "INDIA, USA and UK",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11290755/

相关文章:

Javascript ID 转换为变量 onchange

javascript - 为当前图像/幻灯片分配类别

javascript - 在提交表单时,在 Codeigniter 验证之前先进行 jquery 检查

javascript - 尝试查找标题是否包含单词然后隐藏该父元素

javascript - JQuery 在 Asp.net 中禁用动态创建的 Html 按钮

android - 错误: package PermissionUtils does not exist

javascript - 在垂直列中动态创建 HTML 表单

javascript - Google Map Autocomplete API 不添加地址选项

javascript - JQuery 位置选择器 - 通过选择菜单更新纬度和经度

ruby-on-rails - 谷歌地图在第一次加载时不显示