
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
@codious/ngx-google-places-autocomplete
Advanced tools
A Google Places Autocomplete component for Angular.
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.
npm install @codious/ngx-google-places-autocomplete
npm install @types/google.maps --save-dev
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 {}
Once Google Places Autocomplete is configured, add the custom element <ngx-google-places-autocomplete></ngx-google-places-autocomplete>
in your view to use it.
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.
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]);
});
});
}
}
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>
FAQs
A Google Places Autocomplete component for Angular.
We found that @codious/ngx-google-places-autocomplete demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.