如何将谷歌自动完成结果仅限于城市和国家 [英] How to limit google autocomplete results to City and Country only

查看:30
本文介绍了如何将谷歌自动完成结果仅限于城市和国家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 google 自动完成地方 javascript 为我的搜索框返回建议的结果,我需要的是只显示与输入的字符相关的城市和国家,但 google api 会给出很多我不需要的一般地方结果,那么如何限制结果只显示城市和国家.

我正在使用以下示例:

<头><style type="text/css">身体 {字体系列:无衬线;字体大小:14px;}</风格><title>Google Maps JavaScript API v3 示例:地点自动完成</title><script src="http://maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places" type="text/javascript"></script><script type="text/javascript">函数初始化(){var input = document.getElementById('searchTextField');var autocomplete = new google.maps.places.Autocomplete(input);}google.maps.event.addDomListener(window, 'load', initialize);<身体><div><input id="searchTextField" type="text" size="50" placeholder="输入位置" autocomplete="on">

解决方案

可以试试国家限制

函数初始化(){变量选项 = {类型:['(城市)'],组件限制:{国家:我们"}};var input = document.getElementById('searchTextField');var autocomplete = new google.maps.places.Autocomplete(input, options);}

更多信息:

ISO 3166-1 alpha-2可用于将结果限制为特定组.目前,您可以使用 componentRestrictions 按国家/地区过滤.

国家/地区必须作为两个字符传递,与 ISO 3166-1 Alpha-2 兼容的国家/地区代码.


官方指定的国家代码

I am using google autocomplete places javascript to return suggested results for my searchbox , what I need is to only show the city and the country related to the characters entered but google api will give a lot of general places results which I dont need , so how to limit the result to show only city and the country .

I am using the following Example:

<html>
<head>
<style type="text/css">
   body {
         font-family: sans-serif;
         font-size: 14px;
   }
</style>

<title>Google Maps JavaScript API v3 Example: Places Autocomplete</title>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places" type="text/javascript"></script>
<script type="text/javascript">
   function initialize() {
      var input = document.getElementById('searchTextField');
      var autocomplete = new google.maps.places.Autocomplete(input);
   }
   google.maps.event.addDomListener(window, 'load', initialize);
</script>

</head>
<body>
   <div>
      <input id="searchTextField" type="text" size="50" placeholder="Enter a location" autocomplete="on">
   </div>
</body>
</html>

解决方案

You can try the country restriction

function initialize() {

 var options = {
  types: ['(cities)'],
  componentRestrictions: {country: "us"}
 };

 var input = document.getElementById('searchTextField');
 var autocomplete = new google.maps.places.Autocomplete(input, options);
}

More info:

ISO 3166-1 alpha-2 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.


Officially assigned country codes

这篇关于如何将谷歌自动完成结果仅限于城市和国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆