Google地方信息自动填充-类型:地址,地址,营业地点-不能正常工作

问题描述 投票:1回答:1

我正在尝试在Google地方信息中设置自动完成的类型,这让我发疯了!

我希望我的用户能够通过键入他们正在寻找的地址或营业地点来过滤搜索,但由于某些原因,它无法正常工作。

我正在使用的代码:

<script defer src="https://maps.googleapis.com/maps/api/js?libraries=places&key=[my_api_key]"></script>
<script>
    var searchInputStart = 'start_address';
    start_address = new google.maps.places.Autocomplete((document.getElementById(searchInputStart)), {
        types: ['geocode'],
        types: ['address'],
        types: ['establishment']
    });
    google.maps.event.addListener(start_address, 'place_changed', function () {
        var place_start = start_address.getPlace();
        document.getElementById('loc_lat_start').value = place_start.geometry.location.lat();
        document.getElementById('loc_long_start').value = place_start.geometry.location.lng();
        //get_directions to address
        set_route();
    });
</script>

如果我取出->键入:[地址]->它显示正确的场所,但不显示地址。

如果我取出->键入:[机构]->它显示正确的地址,但不显示机构。

我的用户如何从输入地址或营业地点的位置正确过滤位置?

有人可以帮我吗?我在做什么错?

谢谢。

google-places-api google-geocoder street-address googleplacesautocomplete
1个回答
0
投票

根据Google Maps API文档,您只能在自动完成选项中设置一种类型。

类型 Array<string>:要返回的预测类型。有关受支持的类型的列表,请参见开发人员指南。如果未指定任何内容,则返回所有类型。 [通常只允许使用一种类型。例外是您可以安全地混合使用“地理代码”和“场所”类型,但是请注意,这与不指定类型具有相同的效果。

来源:https://developers.google.com/maps/documentation/javascript/reference/places-widget#AutocompleteOptions

这意味着您可以使用

start_address = new google.maps.places.Autocomplete(
    document.getElementById(searchInputStart), 
    {
        types: ['address']
    }
);

start_address = new google.maps.places.Autocomplete(
    document.getElementById(searchInputStart), 
    {
        types: ['geocode', 'establishment']
    }
);

但不是所有这些对象都在同一选项对象中。

我希望我的回答能澄清您的疑问。

© www.soinside.com 2019 - 2024. All rights reserved.