【问题标题】:location search autocomplete and select city and state位置搜索自动完成并选择城市和州
【发布时间】:2018-02-05 18:23:59
【问题描述】:
       <script>
            function initMap() {

                var input = document.getElementById('searchInput');
                var autocomplete = new google.maps.places.Autocomplete(input);
                var infowindow = new google.maps.InfoWindow();
                autocomplete.addListener('place_changed', function () {
                    infowindow.close();
                    var place = autocomplete.getPlace();
                    if (!place.geometry) {
                        window.alert("Autocomplete's returned place contains no geometry");
                        return;
                    }
                    //Location details
                    for (var i = 0; i < place.address_components.length; i++) {
                        if (place.address_components[i].types[0] === 'postal_code') {
                            document.getElementById('postal_code').value = place.address_components[i].long_name;
                        }
                        if (place.address_components[i].types[0] === 'country') {
                            document.getElementById('country').value = place.address_components[i].long_name;
                        }
                        if (place.address_components[i].types[0] === 'cities') {
                            document.getElementById('cities').value = place.address_components[i].long_name;
                        }
                    }

                    document.getElementById('location').value = place.formatted_address;
                    document.getElementById('lat').value = place.geometry.location.lat();
                    document.getElementById('lon').value = place.geometry.location.lng();
                });
            }
            google.maps.event.addDomListener(window, 'load', initialize);
        </script>

我检索国家名称,
我检索密码
我还可以检索位置纬度和经度
但我无法检索城市
请检查此代码并告诉我我的代码中有什么错误 我也想检索地区和州

【问题讨论】:

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


    【解决方案1】:

    根据地区更改城市:

    if (place.address_components[i].types[2] === 'locality')
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-22
    • 2017-06-14
    • 2023-03-25
    • 2011-05-15
    • 1970-01-01
    • 1970-01-01
    • 2011-01-20
    • 2017-08-27
    相关资源
    最近更新 更多