Angular 13 Google Places Autocomplete Example

Angular 13 google places autocomplete using google maps apis. In this tutorial, i am going to show you how to integrate google places or address autocomplete in angular 13 Application without showing google maps.

Angular 13 Google Places Autocomplete Example

Follow the below given steps to integrate google places or address autocomplete in angular 13 apps without showing maps; as follows:

  • Step 1 – Create New Angular App
  • Step 2 – Install ngx-google-places-autocomplete
  • Step 3 – Get Maps API Key
  • Step 4 – Import Modules in Module.ts File
  • Step 5 – Create Autocomplete Html in View File
  • Step 6 – Import Modules in app.component.ts
  • Step 7 – Start Angular APP

Step 1 – Create New Angular App

Run the following command on terminal to create your angular app using bellow command:

ng new myNewApp

Step 2 – Install ngx-google-places-autocomplete

Run the following command on terminal to install ngx-google-places-autocomplete; as follows:

For npm:

npm install ngx-google-places-autocomplete

For yarn:

yarn add ngx-google-places-autocomplete

Step 3 – Get Maps API Key

In this step, you need to get google api key; So, first, you have to have the Maps API Key, and you can go here to get the complete instructions.

Step 4 – Import Modules in Module.ts File

Go to src/app directory and open app.module.ts file and import the following library into it; as follows:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
import '@angular/compiler';
//import for GooglePlaceModule
import { GooglePlaceModule } from "ngx-google-places-autocomplete";
@NgModule({
declarations: [
	AppComponent,
],
imports: [
	BrowserModule,
	//Adding to imports
	GooglePlaceModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

Step 5 – Create Autocomplete Html in View File

Go to src/app directory and open app.component.html file and add the following HTML markup on it:

<div class="container">
	<h1>Laratutorials</h1>
	<h2>Google Places Autocomplete</h2>
<input ngx-google-places-autocomplete [options]=
'options' (onAddressChange)="AddressChange($event)"/>
{{ formattedaddress }}
</div>

And add library to your index.html in src of your project app; as follows:

<script src=”https://maps.googleapis.com/maps/api/js?key=<Your API KEY>&libraries=places&language=en”></script>

Step 6 – Import Modules in app.component.ts

Go to src/app direcory and open app.component.ts file and import the following modules into it; as follows:

import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'rou';
//Local Variable defined
formattedaddress=" ";
options={
	componentRestrictions:{
	country:["AU"]
	}
}
public AddressChange(address: any) {
//setting address from API to local variable
this.formattedaddress=address.formatted_address
}
}

Step 7 – Start Angular APP

Run application using ng serve –o and you should see Google google autocomplete search into your browser.

Be First to Comment

Leave a Reply

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