Google Address Autocomplete Implementation through JS

Google Address Autocomplete Implementation through JS

This blog post quickly explains you how you can implement google address autocomplete on any form using mere JavaScript. You will need to generate the API key from google cloud console and enable the Places API. In addition to activate the API key you will need to add the billing details to your cloud console. Here is the link that explains how you can generate the Google’s Places API key.

Now let’s look into the code that you need on your end.

First of all let’s look into the HTML part. Below shown is a simple form with a text field where we will implement the Google address autocomplete.

Above HTML have a very few basic elements. On Head, I have added a script that needs to be included for google autocomplete to work. You need to replace “YOUR_API_KEY” with the API key that you generated from google cloud console.

Next script is for jQuery, that is the optional script, which is not necessary but in my current example I will use jQuery to get the address and fill the address fields.

Inside the body there is one input field “searchAddressList” where we will implement google address autocomplete and once customer clicks the address from suggestion we will fill other address fields.

Now the script that performs the autocomplete and when visitor clicks the address it autofills the address fields.

To modify the search results for example, above will return all address, if you want full address with country restrictions replace googleMapInit() function with below code snippet.

So complete code snippet will be as below:

That’s it, you should see the result as below screenshot.

autocomplete demo

If you have problem loading the autocomplete suggestion, try checking your browser console logs and see if there is any issue with permission or billing on your API key.