【问题标题】:How to make google address autofill using API, to replace full address with number & street?如何使用 API 使谷歌地址自动填充,用数字和街道替换完整地址?
【发布时间】:2020-06-20 09:20:58
【问题描述】:
【问题讨论】:
标签:
javascript
html
api
google-maps
google-maps-api-3
【解决方案1】:
尝试如下修改fillInAddress:
function fillInAddress() {
// Get the place details from the autocomplete object.
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.
var streetAddress = '';
for (var i = 0; i < place.address_components.length; i++) {
var addressType = place.address_components[i].types[0];
if (componentForm[addressType]) {
if (addressType === 'street_number') {
streetAddress += place.address_components[i][componentForm[addressType]];
} else if (addressType === 'route') {
if (streetAddress.length) {
streetAddress += ' ';
}
streetAddress += place.address_components[i][componentForm[addressType]];
}
var val = place.address_components[i][componentForm[addressType]];
document.getElementById(addressType).value = val;
document.getElementById('autocomplete').value = streetAddress;
}
}
}
Working jsfiddle 基于the demo。
希望这会有所帮助!