【问题标题】:Google Places API autocomplete zipcode in different input field不同输入字段中的 Google Places API 自动完成邮政编码
【发布时间】:2020-04-02 22:24:04
【问题描述】:

我的网站上有 2 个输入字段,一个用于客户完整地址,另一个用于邮政编码。出于数据库原因,我需要将其作为 2 个不同的输入字段。 我想在我的网站上自动完成。我已经能够让 Google Places API 工作。但我希望它自动完成一个输入字段中的地址并自动填写另一个字段中的邮政编码。

怎么做?

这是我的代码当前代码。

$(document).ready(function() {
    var autocompleteFrom;
    autocompleteFrom = new google.maps.places.Autocomplete((document.getElementById('fromInput')), {
        types: ['address'],
        componentRestrictions: {
            country: 'dk'
        }
    });

【问题讨论】:

    标签: javascript google-maps google-maps-api-3 google-places-api googleplacesautocomplete


    【解决方案1】:

    相关问题:

    Google Maps Javascript API v3 文档中有一个相关示例,可以修改为仅从 AutoComplete 响应中获取邮政编码:Place Autocomplete Address Form

    component_form 对象更改为仅包含postal_code

    var componentForm = {
      postal_code: 'short_name'
    };
    

    然后在place_changed事件触发时调用fillInAddress函数:

    autocompleteFrom.addListener('place_changed', fillInAddress);
    

    proof of concept fiddle

    代码 sn-p:

    $(document).ready(function() {
      var autocompleteFrom;
      var placeSearch;
    
      var componentForm = {
        postal_code: 'short_name'
      };
      autocompleteFrom = new google.maps.places.Autocomplete((document.getElementById('fromInput')), {
        types: ['address'],
        componentRestrictions: {
          country: 'dk'
        }
      });
      // When the user selects an address from the drop-down, populate the
      // address fields in the form.
      autocompleteFrom.addListener('place_changed', fillInAddress);
    
      function fillInAddress() {
        // Get the place details from the autocomplete object.
        var place = autocompleteFrom.getPlace();
        console.log(place);
        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 then fill-in 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;
          }
        }
      }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <label>address</label><input id="fromInput" /><br />
    <label>postal code</label><input id="postal_code" />
    <!-- Replace the value of the key parameter with your own API key. -->
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=places"></script>

    【讨论】:

    • 同一页面有多个输入框怎么办?我有 From address 输入字段和一个 from zip code 输入字段,并且我有一个 to address 和 to zip code 输入字段...我不能同时使用 id="postal_code" 命名 toZipcode og fromZipcode... 如何我改了吗?
    猜你喜欢
    • 2018-05-26
    • 1970-01-01
    • 2020-01-27
    • 2021-11-30
    • 2020-12-13
    • 1970-01-01
    • 2015-11-17
    • 2012-08-29
    • 1970-01-01
    相关资源
    最近更新 更多