Google Places JavaScript Autocomplete:我可以从地名中删除国家吗? [英] Google Places JavaScript Autocomplete: can I remove the country from the place name?

查看:81
本文介绍了Google Places JavaScript Autocomplete:我可以从地名中删除国家吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var city; 

我有以下jQuery代码,可以很好地获取所选国家的城市列表。 var place; $('#city')。on('focus',function(){
input = this,options = {
types:['(cities)'] ,
componentRestrictions:{
country:$('#country option:selected')。val()
}
};
city = new google.maps。 (input,options);
google.maps.event.addListener(city,'place_changed',function(){
place = city.getPlace();
$(input ).val(place.address_components [0] .long_name);
})
})

基本上,一旦这个人选择了这个地方,它就会将输入框中的值替换为没有国家的城市值。

它看起来像一个在用户已经选择国家时,在下拉菜单中有城市,国家的小笨蛋,所以有人知道如果您已经定义了,是否可以显示城市名称一个 componentRestrictions 值将结果限制为a国家?



我发现我的当前设置方法,一旦选择有点...垃圾真的...

解决方案

简短的回答是: Autocomplete 中的响应内容由Google控制在您的一条评论中)和 Autocomplete api-doc 不提供自定义显示的响应文本的方法,而不是更改占位符文本 dev-guide 。你使用的解决方案是关于你拥有的最好的选择(并且有一点被动,但我不知道它是否真的是垃圾)。

除了如果您对这种方法不满意,您可以始终创建自己的输入区域,并自行完成对请求 - 响应的全面控制。您可以直接使用Places库 dev-guide 并完全控制所显示的所有内容。


I have the following jQuery code which works well with getting the city list for the selected country.

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);
    })
})

Basically, once the person selects the place, it replaces the value in the input box with the "city" value without the country.

It looks a little stupid having City, Country in the dropdown menu when the user has already selected a country, so does anybody know if it is possible to display JUST the city name if you have defined a componentRestrictions value restricting the results to a country?

I find my current method of setting it once the selection has been made to be a bit... rubbish really...

解决方案

The short answer is: the response content in the Autocomplete is controlled by Google (you mention this in one of your comments) and the Autocompleteapi-doc doesn't provide a way to customize the response text that is displayed, other than changing the placeholder textdev-guide. The solution you are using is about the best option you have (and is a little reactive, but I don't know if its really "rubbish").

Other than that, if you are less than satisfied with that approach, you could always just create your own input area and take complete control of the request-response back and forth yourself. You can use the Places Librarydev-guide directly and take complete control of everything that is shown.

这篇关于Google Places JavaScript Autocomplete:我可以从地名中删除国家吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆