@codious/ngx-google-places-autocomplete
TypeScript icon, indicating that this package has built-in type declarations

2.0.4 • Public • Published

Codious.Ngx-Google-Places-Autocomplete

Build Status Quality Gate Status

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: '', // your Google API key retrieved from the Google Developer Console
      language: 'nl', // see https://developers.google.com/maps/documentation/javascript/localization
      libraries: 'places', // see https://developers.google.com/maps/documentation/javascript/libraries
      loadScript: true | false, // whether or not the <script> tag of the Google Maps API should be loaded
      options: { types: ['geocode'] }, // see https://developers.google.com/maps/documentation/javascript/places-autocomplete#add_autocomplete
      region: 'US', // see https://developers.google.com/maps/documentation/javascript/localization#Region
    }),
    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>

Package Sidebar

Install

npm i @codious/ngx-google-places-autocomplete

Weekly Downloads

4

Version

2.0.4

License

MIT

Unpacked Size

122 kB

Total Files

24

Last publish

Collaborators

  • gillesv
  • bartlenaerts
  • yluijten
  • pieterwillemsen