How To Add Google Places Autocomplete Address Form In Angular ?

Hello Friends, In this article, we will learn how to Add Google Places Autocomplete Address Form in the Angular Application.
When the user will enter some address text in the input box then it will get the location recommendations and can autocomplete the location. we will use the ngx-google-places-autocomplete angular package for it.

First, we need to install ngx-google-places-autocomplete in the angular project

npm install ngx-google-places-autocomplete

Add the google maps API library in index.html

<script src=”<Your API KEY>&libraries=places&language=en”></script>

Generate an API Key and replace it with that API Key in the above script tag in place of <Your API KEY>.

Add necessary imports of GooglePlaceModule in app.module.ts.

import { GooglePlaceModule } from "ngx-google-places-autocomplete";

  imports: [
  providers: [],
  bootstrap: [AppComponent]
export class AppModule { }

Now we are creating an address form and address auto-complete using google place API.

put the below code in your app.component.html page

<div id="locationField">
    <input type="text" id="autocomplete" placeholder="Search Location" ngx-google-places-autocomplete
        (onAddressChange)="placeChangedCallback($event)" />
<table id="address">
        <td class="label">Street Address</td>
        <td class="slimField">
            <input class="field" [(ngModel)]="street_number" disabled="true" />
        <td class="wideField" colspan="2">
            <input class="field" [(ngModel)]="address" disabled="true" />
        <td class="label">City</td>
        <td class="wideField" colspan="3">
            <input class="field" [(ngModel)]="city" disabled="true" />
        <td class="label">State</td>
        <td class="slimField">
            <input class="field" [(ngModel)]="state" disabled="true" />
        <td class="label">Zip code</td>
        <td class="wideField">
            <input class="field" [(ngModel)]="zip" disabled="true" />
        <td class="label">Country</td>
        <td class="wideField" colspan="3">
            <input class="field" [(ngModel)]="country" disabled="true" />

Now we add a method in the app.compont.ts file when we are select address form suggestion we are filled in address form

put the below code in your app.component.ts file

placeChangedCallback(place: any) {
    this.street_number = "";
    this.address = ""; = "";
    this.state = ""; = ""; = "";

    const addressFrom = {
      street_number: "short_name",
      route: "long_name",
      locality: "long_name",
      sublocality_level_1: "sublocality_level_1",
      administrative_area_level_1: "short_name",
      country: "long_name",
      postal_code: "short_name",

    place.address_components.forEach((add: any) => {
      add.types.forEach((addType: any) => {
        if (addType == "street_number")
          this.street_number = add.short_name;
        if (addType == "route")
          this.address = add.long_name;
        if (addType == "locality" || addType == "sublocality_level_1")
 = add.long_name;
        if (addType == "administrative_area_level_1")
          this.state = add.long_name;
        if (addType == "country")
 = add.long_name;
        if (addType == "postal_code")
 = add.long_name;

Now run the application and search any places and it will suggest places based on your input.



1 Comment

  1. Manodeep Mandal

    Error in app.components.ts


Submit a Comment

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


Select Categories