【问题标题】:Google autocomplete split into separate fields谷歌自动完成拆分成单独的字段
【发布时间】:2018-03-31 06:32:36
【问题描述】:

您好,我有 3 个不同的输入(城市、街道和街道编号)。例如,我的国家是“Polska”,城市是“Warszawa”,所以我只需要显示这个城市的街道。怎么做?

var input = document.getElementById('inputid');

var param = {
    componentRestrictions: {country: 'PL'},
};

autocomplete = new google.maps.places.Autocomplete(input, param);
geocoder = new google.maps.Geocoder();

google.maps.event.addListener(autocomplete, 'place_changed', function() {
    var place = autocomplete.getPlace();
    if(place.address_components) {
        console.log(place.address_components);
    }
});

【问题讨论】:

    标签: google-maps google-maps-api-3 geolocation google-places-api


    【解决方案1】:

    这个很简单。如果您使用来自 Google Place Autocomplete Address Form 的示例代码,您需要做的就是删除不必要的输入字段并保留您想要填充的字段 - 例如:street_number 和 route。您还需要删除不必要的属性,并从 componentForm 对象中保留 street_number 和 route。像这样:

    var componentForm = {
     street_number: 'short_name',
     route: 'long_name'
    };
    

    自动完成对象的第二个参数(选项)中的“componentRestrictions”等附加选项。它应该看起来像这样:

    autocomplete = new google.maps.places.Autocomplete(
       (document.getElementById('autocomplete')),
         {
           types: ['geocode'],
           componentRestrictions : {
            country: 'PL',
           }
         }
    );
    

    有关组件过滤的更多详细信息,您可以查看this链接。

    请注意:

    此示例中特定地址组件的选择基于典型的地址格式。您可能需要选择不同的组件以与不同国家/地区的邮政地址格式保持一致。

    如需了解详情,请点击this链接。

    这是一个现场演示:

          var placeSearch, autocomplete;
          var componentForm = {
            street_number: 'short_name',
            route: 'long_name'
          };
    
          function initAutocomplete() {
            // Create the autocomplete object, restricting the search to geographical
            // location types.
            autocomplete = new google.maps.places.Autocomplete(
                /** @type {!HTMLInputElement} */(document.getElementById('autocomplete')),
                {
                  types: ['geocode'],
                  componentRestrictions : {
                     country: 'PL',
                  }
                }
            );
    
            // When the user selects an address from the dropdown, populate the address
            // fields in the form.
            autocomplete.addListener('place_changed', fillInAddress);
          }
    
          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;
              }
            }
          }
    
          // Bias the autocomplete object to the user's geographical location,
          // as supplied by the browser's 'navigator.geolocation' object.
          function geolocate() {
            if (navigator.geolocation) {
              navigator.geolocation.getCurrentPosition(function(position) {
                var geolocation = {
                  lat: position.coords.latitude,
                  lng: position.coords.longitude
                };
                var circle = new google.maps.Circle({
                  center: geolocation,
                  radius: position.coords.accuracy
                });
                autocomplete.setBounds(circle.getBounds());
              });
            }
          }
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCzjs-bUR6iIl8yGLr60p6-zbdFtRpuXTQ&libraries=places&callback=initAutocomplete"
            async defer></script>
                <div id="locationField">
          <input id="autocomplete" placeholder="Enter your address"
                 onFocus="geolocate()" type="text"></input>
        </div>
    
        <table id="address">
          <tr>
            <td class="label">Street address</td>
            <td class="slimField"><input class="field" id="street_number"
                  disabled="true"></input></td>
            <td class="wideField" colspan="2"><input class="field" id="route"
                  disabled="true"></input></td>
          </tr>
        </table>

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-31
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多