如何将Google地点限制在特定城市 [英] How to restrict google places to specific city
本文介绍了如何将Google地点限制在特定城市的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前只能将地点限制在一个国家/地区,但我也想将地点限制在一个特定的城市.有什么想法要实现吗?这是我当前的代码:
I am currently able to restrict places to only one country, but I also want to restrict on a specific city also. Any ideas how to achieve that? This is my current code:
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);
});
推荐答案
正如Luke所说,地点自动完成功能使您可以使用componentRestrictions仅按国家/地区进行过滤.
As Luke said the places autocomplete allows you to use componentRestrictions to filter by country only.
但是您可以对搜索请求字符串使用一些技巧. 只需在请求中添加带有城市名称的前缀即可.
But you can use a little trick with a search request string. Just add prefix with city name in request.
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;
}
}
});
这篇关于如何将Google地点限制在特定城市的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文