Google API自动填充功能无法识别地址

时间:2016-12-17 10:43:43

标签: javascript google-maps google-geocoder

Courchevel, Saint-Bon-Tarentaise, France出现在搜索结果中,但当我想使用此地址时,geocode无法识别它。

它只识别这一个Courchevel 1650, Saint-Bon-Tarentaise, France

如何使原始地址可以识别?

var placeSearch, autocomplete, destination;
var componentForm = {
    street_number: 'short_name',
    route: 'long_name',
    locality: 'long_name',
    administrative_area_level_1: 'short_name',
    country: 'long_name',
    postal_code: 'short_name'
};

function initAutocomplete() {
    // Create the autocomplete object, restricting the search to geographical
    // location types.
    autocomplete = new google.maps.places.Autocomplete(
        /** @type {!HTMLInputElement} */
        (document.getElementById('from')), {
            types: ['geocode']
        }
    );

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

    destination = new google.maps.places.Autocomplete(
        /** @type {!HTMLInputElement} */
        (document.getElementById('to')), {
            types: ['geocode']
        }
    );

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

// [START region_fillform]
function fillInAddress() {
    var place = autocomplete.getPlace();

    for (var component in componentForm) {
        document.getElementById(component).value = '';
        document.getElementById(component).disabled = false;
    }

    // Get each component of the address from the place details
    // and fill the corresponding field on the form.
    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][componentForm[addressType]];
            document.getElementById(addressType).value = val;
        }
    }
}
// [END region_fillform]

1 个答案:

答案 0 :(得分:0)

我一直试图重现这个问题,在Google Autocomplete Address FormPlace Autocomplete上测试这两个地址。看起来谷歌正试图将你的搜索放在某个地方,只是把它放在一般的高雪维尔地区。&#34;

似乎没有正确的方法可以使用Google API解决此问题,除了使用更精确的地址或reporting the address issue到谷歌并等待他们修复API。 (我认为他们实际上对这些反应很快。)

我尝试使用SmartyStreets验证地址。它符合您的地址,我不确定它是否正确:

enter image description here

我建议使用像SmartyStreets这样的地址API进行验证(意思是,它会检查地址数据以查找已知地址并查明它,而不仅仅是在输入时查找所有搜索字词;它&#39如果地址对你来说比地图更重要,或者如果你正在运送或类似的东西,那么特别方便。

完全披露:我在SmartyStreets工作。