Codious.Ngx-Google-Places-Autocomplete

A Google Places Autocomplete component for Angular.
This plugin is a custom component build with the Google Places AutocompleteService instead of the Google Places Autocomplete class. You can use this plugin easily in a form and don't have to deal with the asynchronous placed_changed
event. Simply bind a formControl
or formControlName
to the component to get the value of the input in your form. The downside is that you still need to do your own geocoding if you want to have geographic coordinates of the address. Luckily, this can be easily done with the Google Geocoder.
Make sure you have the Google Places API Web Service activated in the Google API Console.
Installation
npm install @codious/ngx-google-places-autocomplete
npm install @types/google.maps --save-dev
Configuration
Inside your app.module.ts
file import the plugin in your NgModule
.
@NgModule({
bootstrap: [AppComponent],
declarations: [AppComponent],
imports: [
BrowserModule,
NgxGooglePlacesAutocompleteModule.forRoot({
key: '',
language: 'nl',
libraries: 'places',
loadScript: true | false,
options: { types: ['geocode'] },
region: 'US',
}),
ReactiveFormsModule,
],
})
export class AppModule {}
Usage
Once Google Places Autocomplete is configured, add the custom element <ngx-google-places-autocomplete></ngx-google-places-autocomplete>
in your view to use it.
Google Maps API loaded
The scriptLoaded
event is emitted when the Google Maps API Script is completely loaded. This event is used together with other Angular components in combination with the option loadScript=false
to make sure the Google Maps API Script is loaded only once.
Google Places Autocomplete needs at least the library places. Perhaps the other Angular components that loads the Google Maps API Script doesn't include the library places by default. If so, add it to the libraries option of the other Angular components.
Get the input value
Bind the formControl of formControlName attribute to <ngx-google-places-autocomplete></ngx-google-places-autocomplete>
to get the value selected from the Google Places AutocompleteService in your form. Do your own geocoding if necessary.
<form [formGroup]="formGroup" (ngSubmit)="onSubmit()">
<input formControlName="search" type="text" />
<ngx-google-places-autocomplete formControlName="place"></ngx-google-places-autocomplete>
<button type="submit">Search</button>
</form>
import { Component } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
@Component({
selector: 'app',
templateUrl: './app.component.html',
})
export class AppComponent {
public formGroup = new FormGroup({
place: new FormControl(''),
search: new FormControl(''),
});
public async onSubmit(): Promise<void> {
const place = await this.geocode(this.formGroup.value.place);
console.log(place);
}
private async geocode(address: string): Promise<google.maps.GeocoderResult> {
return new Promise((resolve, reject) => {
new google.maps.Geocoder().geocode({ address }, (results: google.maps.GeocoderResult[], status: google.maps.GeocoderStatus) => {
if (status !== google.maps.GeocoderStatus.OK) reject();
if (status === google.maps.GeocoderStatus.OK) resolve(results[0]);
});
});
}
}
Other attributes
The other attributes that can be used on <ngx-google-places-autocomplete></ngx-google-places-autocomplete>
are:
autoSubmit
: Whether to automatically submit the form after a place is selected from the dropdown.placeholder
: The placeholder shown on the input.
<ngx-google-places-autocomplete autoSubmit="true" formControlName="place" placeholder="Enter a location"></ngx-google-places-autocomplete>