首页 文章

Google Map 自动填充完整地址

提问于
浏览
-2

我在输入字段中使用Google Map 自动填充功能的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() 之类的东西,但这不存在,我没有看到没有复杂循环和数组的方法 .

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

2 回答

  • 2

    你想要formatted_address

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

    您也可以这样做,因为formatted_address在某些情况下不提供完整地址 .

    completeAddress = document.getElementById('autocomplete').value;
    

    其中'autocomplete'是用于搜索地点的输入字段的ID

相关问题