fis*_*era 6 google-places-api google-places
我目前只限制一个国家的地方,但我也想限制一个特定的城市.任何想法如何实现?这是我目前的代码:
var autocomplete,
options = {
types: ['geocode'],
componentRestrictions: {country: 'est'}
};
autocomplete = new google.maps.places.Autocomplete(input, options);
google.maps.event.addListener(autocomplete, 'place_changed', function () {
var place = autocomplete.getPlace().formatted_address;
$(input).attr('value',place);
});
Run Code Online (Sandbox Code Playgroud)
正如Luke所说,地方自动填充功能允许您使用componentRestrictions仅按国家/地区进行过滤.
但是您可以使用搜索请求字符串的小技巧.只需在请求中添加带有城市名称的前缀.
var prefix = 'Kyiv, ';
$(input).on('input',function(){
var str = input.value;
if(str.indexOf(prefix) == 0) {
// string already started with prefix
return;
} else {
if (prefix.indexOf(str) >= 0) {
// string is part of prefix
input.value = prefix;
} else {
input.value = prefix+str;
}
}
});
Run Code Online (Sandbox Code Playgroud)
地方自动填充功能目前允许您使用componentRestrictions按国家/地区进行过滤.在你的情况下我会做的是使用options参数来过滤定义相关城市的边界:
var cityBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(25.341233, 68.289986),
new google.maps.LatLng(25.450715, 68.428345));
var options = {
bounds: cityBounds,
types: ['geocode'],
componentRestrictions: {country: 'est'}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14510 次 |
| 最近记录: |