[英]Google Place Autocomplete Address Form. How to Get each component of the address from the place details?
下一页将带您进入Google Maps API-放置自动完成地址表格,这是您可能会使用的实用示例。
在他的页面中,您可以看到代码示例以及其中的以下子例程:
// 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;
}
}
如您所见,此For循环代码非常动态。
谁能告诉我如何将每个地址字段分别返回到自己的变量或数组中,以便我可以按选择的任何给定顺序将其重新构造成字符串?
您可以使用对象js
var your_obj = {};
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;
your_obj[addresType] = val;
}
}
//and after that you can use it where you uant like:
var postal_code = your_obj['postal_code'];
//check first console.log() to know all items in your_obj
console.log(your_obj);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.