Google Maps Autocomplete-仅城市和邮政编码

时间:2018-07-25 06:33:08

标签: javascript google-maps google-maps-api-3

是否可以将自动完成搜索仅限于城市和邮政编码?

这行不通:

autocomplete = new google.maps.places.Autocomplete(
                    /** @type {!HTMLInputElement} */(document.getElementById('autocomplete')),
                    {types: ['(cities', '(postal_code)'], componentRestrictions: {country: "de"}});

也许这是对的吗?

autocomplete = new google.maps.places.Autocomplete(
                    /** @type {!HTMLInputElement} */(document.getElementById('autocomplete')),
                    {types: ['(regions)'], componentRestrictions: {country: "de"}});

来源:https://developers.google.com/places/supported_types

但是自动填充功能也会在我的受限国家/地区以外显示一些结果,为什么?

enter image description here

一些建议?

解决方案

autocomplete = new google.maps.places.Autocomplete(
                    /** @type {!HTMLInputElement} */(document.getElementById('autocomplete')),
                    {types: ['(regions)'], componentRestrictions: {'country': "de"}});

1 个答案:

答案 0 :(得分:0)

下面的代码是我问题的正确答案。

autocomplete = new google.maps.places.Autocomplete(
                    /** @type {!HTMLInputElement} */(document.getElementById('autocomplete')),
                    {types: ['(regions)'], componentRestrictions: {'country': "de"}});