Google Places JavaScript Autocomplete:我可以从地名中删除国家/地区吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10626727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 10:30:02  来源:igfitidea点击:

Google Places JavaScript Autocomplete: can I remove the country from the place name?

javascriptgoogle-mapsgoogle-places-api

提问by Andrew Willis

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

我有以下 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);
    })
})

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, Countryin 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 componentRestrictionsvalue restricting the results to a country?

City, Country当用户已经选择了一个国家时,在下拉菜单中看起来有点愚蠢,所以有人知道如果你定义了一个componentRestrictions将结果限制在一个国家的值是否可以只显示城市名称?

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

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

回答by Marek Stanley

When invoking Google API, specify attribute "region":

调用 Google API 时,指定属性“region”:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false&key=your_key&language=fr&region=FR"></script>

Then, when you create an Autocompleteobject, specify the country in the componentRestrictionattribute, so that it reflects the region you specified:

然后,当您创建Autocomplete对象时,在componentRestriction属性中指定国家/地区,使其反映您指定的地区:

    new google.maps.places.Autocomplete(
        $("#my-input-field").get(0),
        {
            componentRestrictions: {country: "fr"},
            types: ["(regions)"]
        }
    );

回答by Lukas Jelinek

Also you can help yourself by css - hiding last span (=state) in autocomplete item.

您也可以通过 css 帮助自己 - 在自动完成项目中隐藏最后一个跨度(=状态)。

.pac-item > span:last-child {
    display: none;
}

回答by Sean Mickey

The short answer is: the response content in the Autocompleteis controlled by Google (you mention this in one of your comments) and the Autocompleteapi-docdoesn'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").

简短的回答是: 中的响应内容Autocomplete由 Google 控制(您在其中一条评论中提到了这一点)并且Autocompleteapi-doc不提供自定义显示的响应文本的方法,除了更改占位符文本开发指南。您使用的解决方案是您拥有的最佳选择(并且有点被动,但我不知道它是否真的“垃圾”)。

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-guidedirectly and take complete control of everything that is shown.

除此之外,如果您对这种方法不太满意,您可以随时创建自己的输入区域并自己来回完全控制请求-响应。您可以直接使用 Places Library开发指南并完全控制显示的所有内容。

回答by aussiegeek

I wait 100 milleseconds, and then replace the text box with the content I want.

我等待 100 毫秒,然后将文本框替换为我想要的内容。

Except in my case I want a blank box, but insert a single space.

除了在我的情况下,我想要一个空白框,但插入一个空格。