And*_*lis 11 javascript google-maps google-places-api
我有以下jQuery代码,可以很好地获取所选国家/地区的城市列表.
var city; var place;
$('#city').on('focus',function(){
input = this, options = {
types: ['(cities)'],
componentRestrictions: {
country: $('#country option:selected').val()
}
};
city = new google.maps.places.Autocomplete(input, options);
google.maps.event.addListener(city, 'place_changed', function() {
place = city.getPlace();
$(input).val(place.address_components[0].long_name);
})
})
Run Code Online (Sandbox Code Playgroud)
基本上,一旦人选择了该位置,它就会将输入框中的值替换为没有国家/地区的"城市"值.
City, Country当用户已经选择了一个国家时,在下拉菜单中看起来有点愚蠢,如果您已经定义了componentRestrictions将结果限制在某个国家/地区,是否有人知道是否可以显示城市名称?
一旦选择有点......我找到了我当前设置它的方法......真的很垃圾......
Mar*_*ley 19
调用Google API时,请指定属性"region":
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false&key=your_key&language=fr®ion=FR"></script>
Run Code Online (Sandbox Code Playgroud)
然后,在创建Autocomplete对象时,在componentRestriction属性中指定国家/地区,以便它反映您指定的区域:
new google.maps.places.Autocomplete(
$("#my-input-field").get(0),
{
componentRestrictions: {country: "fr"},
types: ["(regions)"]
}
);
你也可以通过css帮助自己 - 隐藏自动完成项目中的最后一个跨度(=状态).
.pac-item > span:last-child {
display: none;
}
Run Code Online (Sandbox Code Playgroud)