Google地图自动填充完整地址

时间:2015-01-23 15:40:14

标签: javascript google-maps autocomplete google-maps-autocomplete

我在输入字段中使用Google地图自动填充功能的Web应用程序,如示例页面中所示:

但是,他们使用自动填充功能来划分信息并填写表格:

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()这样的东西,但这不存在,我没有看到没有复杂循环和数组的方法。

如何获取用户选择的文本?

1 个答案:

答案 0 :(得分:2)

你想要formatted_address

function fillInAddress() {
  // Get the place details from the autocomplete object.
  var place = autocomplete.getPlace();
  document.getElementById('fulladdress').value = place.formatted_address;
}