如何限制谷歌的自动完成结果为城市和国家唯一 [英] How to limit google autocomplete results to City and Country only
问题描述
我使用谷歌的自动完成地方javascript来返回建议结果我的搜索框,我需要的是只显示城市和相关的字符全国进入,但谷歌的API会给很多一般的地方的结果,我不需要,所以如何限制结果只显示城市和国家。
我使用下面的例子:
< HTML和GT;
< HEAD>
<风格类型=文/ CSS>
身体 {
FONT-FAMILY:无衬线;
字体大小:14px的;
}
< /风格><标题>谷歌地图JavaScript API v3示例:商家信息自动填充< /标题>
<脚本SRC =http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places类型=文/ JavaScript的>< / SCRIPT>
<脚本类型=文/ JavaScript的>
函数初始化(){
VAR输入=的document.getElementById('searchTextField');
无功自动完成=新google.maps.places.Autocomplete(输入);
}
google.maps.event.addDomListener(窗口'负荷',初始化);
< / SCRIPT>< /头>
<身体GT;
< DIV>
<输入ID =searchTextField类型=文字大小=50占位符=上的&GT输入一个位置自动完成=;
< / DIV>
< /身体GT;
< / HTML>
您可以尝试该国限制
函数初始化(){ VAR的选择= {
类型:['(市)'],
componentRestrictions:{国家:我们}
}; VAR输入=的document.getElementById('searchTextField');
无功自动完成=新google.maps.places.Autocomplete(输入,期权);
}
更多信息:
ISO 3166-1的α-2 可以被用来限制结果特定群体。目前,您可以使用componentRestrictions按国家过滤。
该国必须一样两个字符,ISO 3166-1α-2兼容国家code传递。
正式分配的国家codeS
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&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屋!