Google Places Api 自动完成功能不会返回德国的任何邮政编码 (address_components)

问题描述

我尝试让 Google Places Api 正常工作。 我的问题是我在德国没有任何邮政编码?

我正在使用 address_components 并检查任何邮政编码的所有类型(路由、administration_area_level_1、postal_code 等),但什么都没有?我需要从其他地方获取 Zip 吗?地理?

我做错了什么?

Debug

 function initAutocomplete() {
    address1Field = document.querySelector("#txtTest");
    address2Field = document.querySelector("#hiddenStartTown");
    postalField = document.querySelector("#hiddenStartZip");
    // Create the autocomplete object,restricting the search predictions to
    // addresses in the US and Canada.
    autocomplete = new google.maps.places.Autocomplete(address1Field,{
        componentRestrictions: { country: ["de"] },fields: ["address_components","geometry"],types: ["address"],});
    address1Field.focus();
    // When the user selects an address from the drop-down,populate the
    // address fields in the form.
   autocomplete.addListener("place_changed",fillInAddress);

}

function fillInAddress() {
    // Get the place details from the autocomplete object.
    const place = autocomplete.getPlace();
    debugger;
    let address1 = "";
    let postcode = "";

    // Get each component of the address from the place details,// and then fill-in the corresponding field on the form.
    // place.address_components are google.maps.GeocoderAddressComponent objects
    // which are documented at http://goo.gle/3l5i5Mr
    for (const component of place.address_components) {
        const componentType = component.types[0];
        
        switch (componentType) {
            case "street_number": {
                address1 = `${component.long_name} ${address1}`;
                break;
            }

            case "route": {
                address1 += component.short_name;
                break;
            }

            case "postal_code": {
                postcode = `${component.long_name}${postcode}`;
                break;
            }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)