有没有办法限制Google地方信息自动填充以搜索城市的街道? [英] Is there a way to restrict the Google Places Autocomplete to search a city's streets?
问题描述
当使用Google Places Autocomplete
时,我可以限制搜索到城市的街道吗?
您可以设置 Autocomplete
选项来使用 geocode
作为类型,这将限制结果返回到地址:
var input = document.getElementById('searchTextField');
var options = {
界限:yourBounds,
类型:['geocode']
};
autocomplete = new google.maps.places.Autocomplete(input,options);
没有办法将结果限制在 街道上,但是这个将基本达到你想要的。如果您正确设置了 yourBounds
,可能仅限于城市地区,它将使您尽可能地接近目前的状态。
如果您想通过输入城市开始,可以将地图边界绑定到
Autocomplete
界限,在 Autocomplete
中选择一个城市时,它将自动设置其视口。 var options = {
界限:yourBounds,
类型:['(cities)']
};
autocomplete =
new google.maps.places.Autocomplete(input,options);
//这会将地图边界绑定到Autocomplete的边界:
autocomplete.bindTo('bounds',map);
然后,您可以更新 Autocomplete
'类型,类似于上面,它有返回地址:
autocomplete.setTypes([geocode]);
从那里开始,您可以阅读地图库API文档。
Can I restrict the search to a city's streets when using the Google Places Autocomplete
?
You can set the Autocomplete
options to use geocode
as the type, which will restrict the results that are returned to addresses:
var input = document.getElementById( 'searchTextField' );
var options = {
bounds: yourBounds,
types: ['geocode']
};
autocomplete = new google.maps.places.Autocomplete( input, options );
There is no way to restrict the results to just streets, but this will largely achieve what you want. If you set yourBounds
correctly, maybe limited to the area of a city, it will get you as close as currently possible.
Update responding to the comment:
If you want to start by entering a city, you can bind the map's bounds to the Autocomplete
s bounds, which will have the map set its viewport automatically when a city is selected in the Autocomplete
:
var options = {
bounds: yourBounds,
types: ['(cities)']
};
autocomplete =
new google.maps.places.Autocomplete( input, options );
// This will bind the map's bounds to the Autocomplete's bounds:
autocomplete.bindTo('bounds', map);
Then, you can update the Autocomplete
's types, similar to the above, to have it return addresses:
autocomplete.setTypes( [ "geocode" ] );
From there, you can have a read of the Places Library API Docs.
这篇关于有没有办法限制Google地方信息自动填充以搜索城市的街道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!