【问题标题】:retrieve the postal code along with the address using the Google Places Autocomplete API使用 Google Places Autocomplete API 检索邮政编码和地址
【发布时间】:2018-11-02 17:36:57
【问题描述】:

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

【问题讨论】:

  • 你能发布你的代码吗?
  • 公共功能建议_post(){ $search = $this-&gt;post('search'); $arrContextOptions=array("ssl"=&gt;array("verify_peer"=&gt;false, "verify_peer_name"=&gt;false,),); $url = "<a href="/default/index/tourl?u=aHR0cHM6Ly9tYXBzLmdvb2dsZWFwaXMuY29tL21hcHMvYXBpL3BsYWNlL2F1dG9jb21wbGV0ZS9qc29uP2lucHV0PSUyMi51cmxlbmNvZGUoJTI0c2VhcmNoKS4lMjImdHlwZXM9KHJlZ2lvbnMpJmtleT1BSXphU3lDVG9nYXR4YmdTc2xyY2VoOHRpNWJLbzRUQUNMMnh3cDAmbGlicmFyaWVzPXBsYWNlcw%3D%3D" rel="nofollow" target="_blank">maps.googleapis.com/maps/api/place/autocomplete/…</a>"; $json = file_get_contents($url, false, stream_context_create($arrContextOptions)); $results = json_decode($json); $this-&gt;response(['predictions' =&gt; $results-&gt;predictions]); }
  • 谢谢@Ryankozak。我之前检查了该链接,但它不适用于我的代码。在 url 中使用 types=(regions) 就可以了。谢谢你的帮助。对不起,如果这是一个重复的问题

标签: php reactjs codeigniter google-maps-api-3 googleplacesautocomplete


【解决方案1】:

请通过添加您的 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>

【讨论】:

    猜你喜欢
    • 2023-01-10
    • 2020-12-13
    • 2020-07-19
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    相关资源
    最近更新 更多