-2

我正在使用 Places Autocomplete Google API 传递 - 输入和密钥。我键入一个发送到 API 的地址(搜索文本),然后返回该文本的所有匹配项(地址),我从中选择一个。返回的地址没有邮政编码。我怎么才能得到它?我究竟做错了什么

4

1 回答 1

2

请通过添加您的 api 密钥来尝试以下代码,它对我来说工作正常

$(document).on('click', "#autocomplete", function () {
        var currentInp = $(this).attr("id");
        var $tabletextbox = $(this);
        var autocomplete = new google.maps.places.Autocomplete(document.getElementById(currentInp));
        autocomplete.addListener('place_changed', function () {
            var place = autocomplete.getPlace();
            if (!place.geometry) {
                // User entered the name of a Place that was not suggested and
                // pressed the Enter key, or the Place Details request failed.
                window.alert("No details available for input: '" + place.name + "'");
                return;
            }
            for (var i = 0; i < place.address_components.length; i++) {
              if(place.address_components[i].types[0] == 'postal_code'){
                alert(place.address_components[i].long_name);
              }
            }
            $('#lat').val(place.geometry.location.lat().toFixed(8));
            $('#lng').val(place.geometry.location.lng().toFixed(8));

        });
    });
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=Your API Key&libraries=places"></script>
于 2018-05-24T06:42:06.930 回答