Google API自动填充功能

时间:2017-07-24 09:18:52

标签: javascript jquery html google-maps

我遇到了一个简单的问题。我可以使用谷歌API并获取地址,但我不希望在单独的框中有街道号码和街道地址,我希望街道号码与街道地址相结合并放在同一个框中。任何方向或建议将不胜感激。

目前的结果; enter image description here

这就是我想要的最终结果; enter image description here

我的代码;

HTML

   <body>
      <div class='container'>
         <div class='row'>
          <h2>Example</h2>
            <p>User enters: "3 Dennis", a list of available options appears:</p>
            <ul>
               <li>3 Dennis Vale Drive, Daisy Hill, Queensland, Australia</li>
               <li>etc.</li>
            </ul>
            </div>
         <div class='row'>
            <div class='col-xs-4 text-right'>Address</div>
            <div class='col-xs-6'><input type='text' class='form-control address-field' id='street_number'></div>
         </div>
         <div class='row'>
            <div class='col-xs-4 text-right'>Suburb</div>
            <div class='col-xs-6'><input type='text' class='form-control suburb-field' id='locality'></div>
         </div>
         <div class='row'>
            <div class='col-xs-4 text-right'>State</div>
            <div class='col-xs-6'><input type='text' class='form-control state-field' id='administrative_area_level_1'></div>
         </div>
         <div class='row'>
            <div class='col-xs-4 text-right'>Post Code</div>
            <div class='col-xs-6'><input type='text' class='form-control postcode-field' id='postal_code'></div>
         </div>
      </div>

的Javascript

      var placeSearch, autocomplete;
      var componentForm = {
        street_number: 'short_name',
        locality: 'long_name',
        administrative_area_level_1: 'long_name',
        postal_code: 'short_name'
      };

      function initAutocomplete() {
        // Create the autocomplete object, restricting the search to geographical
        // location types.
        autocomplete = new google.maps.places.Autocomplete(
            /** @type {!HTMLInputElement} */(document.getElementById('street_number')),
            {types: ['geocode']});

        // 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>
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD_Qf8TYV9CLEngR4Fioj-S9_QVX9amC7Y&libraries=places&callback=initAutocomplete"
        async defer></script>

1 个答案:

答案 0 :(得分:2)

您可以在component 词典中添加名为route的新componentForm

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

route属性包含street_address

long_name:"Dennis Vale Drive"

route属性可以存储在hidden输入中。

<input id="route" style="display:none"/>

完成填充所有输入后,只需连接street_number输入和route输入的值。元件。

document.getElementById('street_number').value=document.getElementById('street_number').value+' '+document.getElementById('route').value;

这是full solution.