Autofill Address form using Google palces API in JavaScript

Step 1 : Create an HTML file and write the following code.

  1. <div class="row">  
  2.     <form role="form">  
  3.         <div class="form-group">  
  4.             <label for="email">Full Address:</label>  
  5.             <input type="text" id="FullAddress" class="form-control" />  
  6.         </div>  
  7.         <div class="form-group">  
  8.             <label for="email">Country Name:</label>  
  9.             <input type="text" id="CountryName" class="form-control" />  
  10.         </div>  
  11.         <div class="form-group">  
  12.             <label for="email">State Name:</label>  
  13.             <input type="text" id="StateName" class="form-control" />  
  14.         </div>  
  15.         <div class="form-group">  
  16.             <label for="email">City Name:</label>  
  17.             <input type="text" id="CityName" class="form-control" />  
  18.         </div>  
  19.         <div class="form-group">  
  20.             <label for="email">Zipcode:</label>  
  21.             <input type="text" id="Zipcode" class="form-control" />  
  22.         </div>  
  23.         <button type="submit" class="btn btn-default">Submit</button>  
  24.     </form>  
  25. </div> 
Step 2: Add Google places API reference in your page.
  1. <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>   
Step 3 : Next put this JavaScript code inside the head tag.

  1. <script type="text/javascript">  
  2.     var placeSearch, autocomplete;  
  3.   
  4.     var addressForm = {  
  5.         CityName: 'locality',  
  6.         StateName: 'administrative_area_level_1',  
  7.         CountryName: 'country',  
  8.         Zipcode: 'postal_code'  
  9.     };  
  10.     var componentForm = {  
  11.         locality: 'long_name',  
  12.         administrative_area_level_1: 'long_name',  
  13.         country: 'long_name',  
  14.         postal_code: 'short_name'  
  15.     };  
  16.   
  17.     initAutocomplete();  
  18.     function initAutocomplete() {  
  19.         debugger;  
  20.         autocomplete = new google.maps.places.Autocomplete((document.getElementById('FullAddress')),  
  21.             { types: ['geocode'] });  
  22.         autocomplete.addListener('place_changed', fillInAddress);  
  23.     }  
  24.   
  25.     function fillInAddress() {  
  26.         var place = autocomplete.getPlace();  
  27.         hfLatitude.val(place.geometry.location.lat());  
  28.         hfLongitude.val(place.geometry.location.lng());  
  29.   
  30.         for (var component in addressForm) {  
  31.             document.getElementById(component).value = '';  
  32.             document.getElementById(component).disabled = false;  
  33.         }  
  34.         for (var i = 0; i < place.address_components.length; i++) {  
  35.             var addressType = place.address_components[i].types[0];  
  36.             if (componentForm[addressType]) {  
  37.                 var val = place.address_components[i][componentForm[addressType]];  
  38.                 document.getElementById(getComponentKey(addressType)).value = val;  
  39.             }  
  40.         }  
  41.     }  
  42.     function getComponentKey(val) {  
  43.         for (var key in addressForm) {  
  44.             this_val = addressForm[key];  
  45.             if (this_val == val) {  
  46.                 return key;  
  47.                 break;  
  48.             }  
  49.         }  
  50.     }  
  51. </script>