【问题标题】:Google Maps autocomplete full address谷歌地图自动填充完整地址
【发布时间】:2015-03-22 16:45:12
【问题描述】:

我有一个在输入字段中使用 Google 地图自动完成功能的网络应用程序,例如示例页面:

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

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

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

【问题讨论】:

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


    【解决方案1】:

    你想要formatted_address

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

    【讨论】:

      【解决方案2】:

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

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

      “自动完成”是用于搜索地点的输入字段的 ID

      【讨论】:

        猜你喜欢
        • 2016-10-17
        • 1970-01-01
        • 2014-10-30
        • 2021-04-12
        • 2013-03-23
        • 2017-09-16
        • 2018-04-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多