npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@codious/ngx-google-places-autocomplete

v2.0.4

Published

A Google Places Autocomplete component for Angular.

Downloads

37

Readme

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>