Google地方自动填充功能不会返回管理级别

时间:2016-10-31 09:19:45

标签: google-maps autocomplete google-places-api google-places

我正在使用Google地方自动填充功能获取有关指定地址的详细信息。它一直运作到昨天。今天,当我输入一些街道时,自动完成功能会返回有关街道,城市,国家/地区的信息,但不会返回行政级别。只有当我输入城市(没有街道)时才返回管理级别。几周前,它也恢复了街道的行政级别。

有人知道,如果谷歌改变了什么吗?

感谢您的帮助。

我的代码在这里:

    <script>
    function initAutocomplete() {
        // Create the autocomplete object, restricting the search to geographical
        // location types.
        autocomplete = new google.maps.places.Autocomplete(
                (document.getElementById('autocomplete')),
                {
                    types: ['geocode', 'establishment']
                }
        );

        var input = document.getElementById('autocomplete');

        google.maps.event.addDomListener(input, 'keydown', function(e) {
            if (e.keyCode == 13) { // do not send form when press enter
                e.preventDefault();
            }
        });

        // When the user selects an address from the dropdown, populate the address
        // fields in the form.
        autocomplete.addListener('place_changed', fillInAddress);
    }

    function fillInAddress() {
        // Get the place details from the autocomplete object.
        var place = autocomplete.getPlace();

        console.log(place.address_components);

        var componentForm = {
            street_number: 'order-street-number',
            premise: 'order-street-number',
            route: 'order-street',
            neighborhood: 'order-city-district',
            sublocality_level_1: 'order-city',
            locality: 'order-city',
            administrative_area_level_1: 'order-region',
            country: 'order-country',
            postal_code: 'order-zip'
        };

        $('.order-input').val('');

        for (var i = 0; i < place.address_components.length; i++) {
            var addressType = place.address_components[i].types[0];
            if (componentForm[addressType]) {
                var val = place.address_components[i]['long_name'];
                if (!($('#' + componentForm[addressType]).val())) { // if input is empty, then add value (because of priority, e.g. street_number > premise)
                    $('#' + componentForm[addressType]).val(val);
                }
            }
        }
    }
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=my-key&libraries=places&callback=initAutocomplete"
    async defer></script>

0 个答案:

没有答案
相关问题