Create Place Auto Complete Address Form With Google Maps API

How to auto complete address form with google maps API

Auto complete address form , Hi…. welcome in infinitricks. I will show you how to make auto complete address to your form. There are several ways how to create it. But this time i will show you make auto complete address with Google Maps API.

Why with google maps API?

If we use google maps API, we get a lot of data that can not input into the database one by one . By using this way, we can easily to make it without address data in database. But not forever the data we get from maps can auto complete to all available form. It would not hurt us to try whether it is worth it or not for our app.

How to make it?

First you must have google places API webservice key. You can get your key in here. Next this is the example code, (API KEY on the lastest script tag):

<!-- sumber: -->
<!DOCTYPE html>
    <title>Place Autocomplete Address Form With Google Maps API</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="">
    <script src=""></script>
    <script src=""></script>
    <script src=""></script>

    <link type="text/css" rel="stylesheet" href=",400,500">

    <div class="container">
    <h2>Place Autocomplete Address Form With Google Maps API</h2>
    <div id="locationField">
      <input id="autocomplete" placeholder="Enter your address"
             onFocus="geolocate()" type="text"></input>

    <table id="address" class="table">
        <td class="label">Street address</td>
        <td class="slimField"><input class="field" id="street_number"
        <td class="wideField" colspan="2"><input class="field" id="route"
        <td class="label">City</td>
        <!-- Note: Selection of address components in this example is typical.
             You may need to adjust it for the locations relevant to your app. See
        <td class="wideField" colspan="3"><input class="field" id="locality"
        <td class="label">State</td>
        <td class="slimField"><input class="field"
              id="administrative_area_level_1" disabled="true"></input></td>
        <td class="label">Zip code</td>
        <td class="wideField"><input class="field" id="postal_code"
        <td class="label">Country</td>
        <td class="wideField" colspan="3"><input class="field"
              id="country" disabled="true"></input></td>

      // This example displays an address form, using the autocomplete feature
      // of the Google Places API to help users fill in the information.

      // This example requires the Places library. Include the libraries=places
      // parameter when you first load the API. For example:
      // <script src="">

      var placeSearch, autocomplete;
      var componentForm = {
        street_number: 'short_name',
        route: 'long_name',
        locality: 'long_name',
        administrative_area_level_1: 'short_name',
        country: '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('autocomplete')),
            {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
    <script src=""
        async defer></script>

To see results click here. hopefully help you and see you in next post. Bye

1 thought on “Create Place Auto Complete Address Form With Google Maps API

Leave a Reply

Your email address will not be published.Required fields are marked *