Google自动填充功能拆分为单独的字段

时间:2017-10-19 08:57:12

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

您好我有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);
    }
});

1 个答案:

答案 0 :(得分:0)

这个很简单。如果您使用的是 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>

希望它有所帮助!