Krishneil
Krishneil

Reputation: 1490

Google API Autocomplete

I am stuck with a simple problem. I am able to use google API and get the address, but I don't want to have street number and street address in the separate boxes, I want the street number to be combined with street address and placed in the same box. Any direction or advice will be greatly appreciated.

Current result; enter image description here

And this is what I want the end result to be; enter image description here

My code;

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>

Upvotes: 0

Views: 80

Answers (1)

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48427

You can add a new component called route in your componentForm dictionary.

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

route property contains the street_address.

long_name:"Dennis Vale Drive"

The route property can be stored in a hidden input.

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

When you finished to fill all the inputs just concat the values from street_number input and route input. elements.

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

Here is full solution.

Upvotes: 2

Related Questions