Search code examples
javascriptgoogle-mapsgoogle-maps-api-3google-places-apigoogle-places-autocomplete

Google Places API autocomplete zipcode in different input field


I have 2 input fields on my website, one for the customers full address and the other is for the zip code. I need to have it as 2 different input fields, for database reasons. I want to have autocomplete on my website. I have been able to get Google Places API to work. But I want it to autocomplete the address in one input field and automatically the zip code in another field.

How to do it?

This is my code current code.

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

Solution

  • related questions:

    There is a related example in the Google Maps Javascript API v3 documentation that can be modified to just get the postal code from the AutoComplete response: Place Autocomplete Address Form

    Change the component_form object to just include the postal_code:

    var componentForm = {
      postal_code: 'short_name'
    };
    

    Then call the fillInAddress function when the place_changed event fires:

    autocompleteFrom.addListener('place_changed', fillInAddress);
    

    proof of concept fiddle

    screenshot of result

    code snippet:

    $(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>