How to Implement Address Autocomplete in Node Js using Google Places API?




How to Implement Address Autocomplete in Node Js using Google Places API?

Hello Friends,

This article goes into detail on how to implement address autocomplete in node js using google places API. you can see how to implement google places API in your application using node js. you can see the integration of google places search API in node js. This tutorial will give you a simple example of node js place auto-complete with code examples.

Autocomplete is a feature of the Places library in the Maps JavaScript API. You can use autocomplete to give your applications the type-ahead-search behavior of the Google Maps search field. The autocomplete service can match full words and substrings, resolving place names, addresses, and plus codes.

Step 1: Create Google key

Follow the below given instructions to create a google places autocomplete key:

You need the required google API key for the google places to autocomplete example, so click the link and create your API key Get Google API Key

Then click the Get Google API Key link, after the page looks like this. In this step you need to create a project, so click on the creative project and create your project:

After successfully creating a project, the second thing sees the side menu bar and click credentials. Here you create a google API key:

In this step you will set HTTP Referrers like http://localhost/autoAddress.html , look like this picture :

After successfully create API key , click on the top google dashboard search bar and search Place API, and Enable the Places API.

Step 2: Create Node Express js App

Execute the following command on the terminal to create the node js app:

mkdir my-app

cd my-app

npm init -y

Step 3: Install express body-parser Modules

Execute the following command on the terminal to install express ejs body-parser MySQL modules:

npm install express body-parser --save

body-parser – Node.js request body parsing middleware which parses the incoming request body before your handlers, and makes it available under req.body property. In other words, it simplifies incoming requests.

Express – Express. js is a framework based on Node. js which is used for building web-application using approaches and principles of Node.

Step 4: Create Server.js File And Import Modules

Create server.js file; so visit your app root directory and create server.js file; Then import above-installed modules into it:

var createError = require('http-errors');

var express = require('express');

var path = require('path');

var bodyParser = require('body-parser');

var app = express();



//__dirname : It will resolve to your project folder.


// port must be set to 8080 because incoming http requests are routed from port 80 to port 8080

app.listen(3000, function () {

console.log('Node app is running on port 3000');


module.exports = app;

Step 5: Create Goolge Places Autocomplete HTML Markup

Create HTML markup page for populating dependent dropdown from database in node js express; So visit root directory and create an index.ejs. Then add the following code into it:

<!doctype html>

<html lang="en">


<title>How to Implement Address Autocomplete in Node Js using Google Places API?</title>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<link rel="stylesheet" href="">

<script src=""></script>

<script src=""></script>



<div class="container mt-5">

<div class="row">

<div class="col-xl-6 col-lg-6 col-md-8 col-sm-12 col-12 m-auto">

<div class="card shadow">

<div class="card-header bg-primary">

<h5 class="card-title text-white">Node js Google Places Autocomplete Address Example</h5>


<div class="card-body">

<div class="form-group">

<label for="autocomplete"> Location/City/Address </label>

<input type="text" name="autocomplete" id="autocomplete" class="form-control" placeholder="Select Location">









Step 6: Create Script For Google Places Autocomplete Address

Implement script for google places autocomplete address; so open your HTML file and the following code into it:

<script src="" async defer></script>

<script type="text/javascript">

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();



Important Note:- Replace the following key with your google key; as shown below:

<script src="" type="text/javascript"></script>

Step 7: Start App Server

You can use the following command to start the node js app server:

//run the below command

npm start

After running this command open your browser and hit

I hope it can help you...

#Node JS