Google 地图自动完成完整地址
Google Maps autocomplete full address
我有一个 Web 应用程序在示例页面中的输入字段中使用 Google 地图自动完成功能:
- https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform
但是,他们使用自动完成来划分信息并填写表格:
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.
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;
}
}
}
这不是我要找的。我需要的是一个简单的 javascript 变量,其中包含该输入字段的内容。类似 var completeAddress = autocomplete.getFullAddress()
的东西,但这不存在,而且我没有看到没有复杂循环和数组的方法。
如何获取用户选择的文本?
function fillInAddress() {
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();
document.getElementById('fulladdress').value = place.formatted_address;
}
您也可以这样做,因为 formatted_address 在某些情况下不会给出完整地址。
completeAddress = document.getElementById('autocomplete').value;
Where 'autocomplete' is the Id of the input field for searching for places
我有一个 Web 应用程序在示例页面中的输入字段中使用 Google 地图自动完成功能:
- https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform
但是,他们使用自动完成来划分信息并填写表格:
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.
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;
}
}
}
这不是我要找的。我需要的是一个简单的 javascript 变量,其中包含该输入字段的内容。类似 var completeAddress = autocomplete.getFullAddress()
的东西,但这不存在,而且我没有看到没有复杂循环和数组的方法。
如何获取用户选择的文本?
function fillInAddress() {
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();
document.getElementById('fulladdress').value = place.formatted_address;
}
您也可以这样做,因为 formatted_address 在某些情况下不会给出完整地址。
completeAddress = document.getElementById('autocomplete').value;
Where 'autocomplete' is the Id of the input field for searching for places