如何使用 API 使 google 地址自动填充,以用数字和街道替换完整地址?
How to make google address autofill using API, to replace full address with number & street?
我正在使用 google API 自动填写地址表。现在我找到了填写完整地址的代码,它会自动填充图片 link #1 中的所有字段,如图所示。我隐藏了不需要的街道号码和路线字段。
我要找的是 select 地址之后,我希望我输入的地址栏不在该字段栏中显示城市、州和国家/地区。我希望自动填充看起来像图片 link #2 中显示的那样,其中地址字段仅用数字和街道替换完整地址,不包括城市、州、国家/地区。当我输入 select 图片 link #3 中显示的地址时,它会自动填充图片 link #2 中显示的内容。
有谁知道如何在点击地址后仅用数字和街道替换完整地址?我在这里使用了 html 和来自 link 的 jscript:
https://github.com/solodev/address-auto-complete
图片Link#1:https://imgur.com/a/oCAAs4C
图片Link#2:https://imgur.com/a/psaz3J4
图片Link#3:https://imgur.com/a/xQMNjpy
尝试修改 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 based off of the demo.
希望对您有所帮助!
我正在使用 google API 自动填写地址表。现在我找到了填写完整地址的代码,它会自动填充图片 link #1 中的所有字段,如图所示。我隐藏了不需要的街道号码和路线字段。
我要找的是 select 地址之后,我希望我输入的地址栏不在该字段栏中显示城市、州和国家/地区。我希望自动填充看起来像图片 link #2 中显示的那样,其中地址字段仅用数字和街道替换完整地址,不包括城市、州、国家/地区。当我输入 select 图片 link #3 中显示的地址时,它会自动填充图片 link #2 中显示的内容。
有谁知道如何在点击地址后仅用数字和街道替换完整地址?我在这里使用了 html 和来自 link 的 jscript: https://github.com/solodev/address-auto-complete
图片Link#1:https://imgur.com/a/oCAAs4C
图片Link#2:https://imgur.com/a/psaz3J4
图片Link#3:https://imgur.com/a/xQMNjpy
尝试修改 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 based off of the demo.
希望对您有所帮助!