我正在使用google autocomplete place javascript返回搜索框的建议结果,我只需要显示与输入字符有关的城市和国家/地区,但google api会提供很多我不需要的常规地方结果,所以将结果限制为仅显示城市和国家。
我正在使用以下示例:
<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>
您可以尝试国家/地区限制
function initialize() {
var options = {
types: ['(cities)'],
componentRestrictions: {country: "us"}
};
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兼容的国家/地区代码传递。
官方指定的国家/地区代码
<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="https://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 options = {
types: ['geocode'] //this should work !
};
var autocomplete = new google.maps.places.Autocomplete(input, options);
}
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>
var options = {
types: ['geocode'] //this should work !
};
var autocomplete = new google.maps.places.Autocomplete(input, options);
对其他类型的引用:http : //code.google.com/apis/maps/documentation/places/supported_types.html#table2
尝试这个
<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="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places®ion=in" 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>
http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places “ type =” text / javascript“
更改为:“ region = in”(印度)
“ http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places®ion=in ” type =“ text / javascript”
弗朗索瓦给出的答案导致列出一个国家的所有城市。但是,如果要求列出一个国家或该国家/地区中的所有地区(城市+州+其他地区等),则可以通过更改类型来相应地过滤结果。
只列出该国的城市
var options = {
types: ['(cities)'],
componentRestrictions: {country: "us"}
};
列出全国所有城市,州和地区
var options = {
types: ['(regions)'],
componentRestrictions: {country: "us"}
};
列出所有机构,例如该国的餐馆,商店和办公室
var options = {
types: ['establishment'],
componentRestrictions: {country: "us"}
};
有关更多信息和选项,请访问:
https://developers.google.com/maps/documentation/javascript/places-autocomplete
希望这对某人有用
与接受的答案基本相同,但已更新为新类型和多个国家/地区限制:
function initialize() {
var options = {
types: ['(regions)'],
componentRestrictions: {country: ["us", "de"]}
};
var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input, options);
}
使用'(regions)'
代替'(cities)'
允许按邮政编码以及城市名称进行搜索。
请参阅官方文档,表3:https :
//developers.google.com/places/supported_types
我一直在使用Google Autocomplete API,这是我发现的最佳解决方案,可以将您的结果限制为仅适用于以下国家/地区:
var autocomplete = new google.maps.places.Autocomplete(input, options);
var result = autocomplete.getPlace();
console.log(result); // take a look at this result object
console.log(result.address_components); // a result has multiple address components
for(var i = 0; i < result.address_components.length; i += 1) {
var addressObj = result.address_components[i];
for(var j = 0; j < addressObj.types.length; j += 1) {
if (addressObj.types[j] === 'country') {
console.log(addressObj.types[j]); // confirm that this is 'country'
console.log(addressObj.long_name); // confirm that this is the country name
}
}
}
如果查看返回的结果对象,您会看到有一个address_components数组,其中包含几个代表地址不同部分的对象。在每个这些对象中,它将包含一个“类型”数组,并且在该“类型”数组中,您将看到与地址关联的不同标签,其中包括一个国家/地区的标签。
另外,由于您所在的国家/地区限制,您将需要缩放地图并居中!
只需使用缩放和中心参数即可!;)
function initialize() {
var myOptions = {
zoom: countries['us'].zoom,
center: countries['us'].center,
mapTypeControl: false,
panControl: false,
zoomControl: false,
streetViewControl: false
};
... all other code ...
}
我为自己找到了解决方案
var acService = new google.maps.places.AutocompleteService();
var autocompleteItems = [];
acService.getPlacePredictions({
types: ['(regions)']
}, function(predictions) {
predictions.forEach(function(prediction) {
if (prediction.types.some(function(x) {
return x === "country" || x === "administrative_area1" || x === "locality";
})) {
if (prediction.terms.length < 3) {
autocompleteItems.push(prediction);
}
}
});
});
此解决方案仅显示城市和国家。
<html>
<head>
<title>Example Using Google Complete API</title>
</head>
<body>
<form>
<input id="geocomplete" type="text" placeholder="Type an address/location"/>
</form>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://ubilabs.github.io/geocomplete/jquery.geocomplete.js"></script>
<script>
$(function(){
$("#geocomplete").geocomplete();
});
</script>
</body>
</html>
有关更多信息,请访问此链接
文章标签:autocomplete , google-maps-api-3 , javascript
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!