项目作者: ueler

项目描述 :
Angular component for Google Maps Places Autocomplete
高级语言: TypeScript
项目地址: git://github.com/ueler/ng-google-places-autocomplete.git
创建时间: 2020-02-09T12:22:49Z
项目社区:https://github.com/ueler/ng-google-places-autocomplete

开源协议:MIT License

下载


@ueler/ng-google-places-autocomplete

Angular component for Google Maps Places autocomplete.

npm version
Actions Status

Introduction

This library offers a component
to easily integrate a Google Maps Places autocomplete typeahead into your project.

Features

  • Lightweight (only 4KB gzipped)
  • Uses Bootstrap styling classes
  • Configurable request options (e.g. to limit the results to specific country)
  • Configurable places details options (e.g. get longitude/latitude from selected place)
  • Returns places details (including long,lat,etc.) when user selects an option
  • Loads the Google Maps Javascript API lazily (only when the component is used, i.e. it doesn’t add to the bundle size upfront)

Stackblitz Example App

See the component in action in this example app:
https://stackblitz.com/edit/angular-bzuvbk

Installation

The library relies on the ngx-bootstrap typeahead component.
Therefore the first step is to install that peer dependency.
(Note: The library is tree-shakeable, therefore only using the typeahead module doesn’t add much to your application size.)

1. Install ngx-bootstrap:

  1. npm install ngx-bootstrap --save

2. Add typeahead package to NgModule imports:

  1. import {TypeaheadModule} from 'ngx-bootstrap/typeahead';
  2. @NgModule({
  3. ...
  4. imports: [TypeaheadModule.forRoot(),...]
  5. ...
  6. })

3. Install @ueler/ng-google-places-autocomplete

  1. npm install @ueler/ng-google-places-autocomplete --save

4. Import module and configure API Key:

You need a Google Maps API Key in order to run places search queries.
Please follow this guide on how to get one: Get API Key
(you need to select the APIs Places API and Maps JavaScript API for the key).
Provide the API key as config to your module:

  1. import {
  2. NG_GOOGLE_PLACES_AUTOCOMPLETE_SETTINGS,
  3. NgGooglePlacesAutocompleteSettings
  4. } from '@ueler/ng-google-places-autocomplete';
  5. @NgModule({
  6. providers: [
  7. {
  8. provide: NG_GOOGLE_PLACES_AUTOCOMPLETE_SETTINGS,
  9. useValue: {
  10. googleMapsApiKey: 'GOOGLE_MAPS_API_KEY_HERE'
  11. } as NgGooglePlacesAutocompleteSettings,
  12. },
  13. ]
  14. })
  15. export class MyModule {
  16. }

Usage

Basic usage:

  1. <lib-ng-google-places-autocomplete></lib-ng-google-places-autocomplete>

Perform an action when user selects an option:

  1. <lib-ng-google-places-autocomplete (addressChanged)="addressChanged($event)">
  2. </lib-ng-google-places-autocomplete>

The $event contains the selected option of the type PlaceResult (https://developers.google.com/maps/documentation/javascript/reference/places-service#PlaceResult).

Configuration Options

Global configuration

In the module via the NgGooglePlacesAutocompleteSettings interface. You can set the locale and the Google Maps Api Key.

  1. import {
  2. NG_GOOGLE_PLACES_AUTOCOMPLETE_SETTINGS,
  3. NgGooglePlacesAutocompleteSettings
  4. } from '@ueler/ng-google-places-autocomplete';
  5. @NgModule({
  6. providers: [
  7. {
  8. provide: NG_GOOGLE_PLACES_AUTOCOMPLETE_SETTINGS,
  9. useValue: {
  10. googleMapsApiKey: 'YOUR_GOOGLE_MAPS_API_KEY',
  11. locale: 'de'
  12. } as NgGooglePlacesAutocompleteSettings
  13. },
  14. ]
  15. })
  16. export class MyModule {
  17. }

Local configuration

Autocompletion results

An optional configuration can be passed to the component to configure the typeahead results

  1. <lib-ng-google-places-autocomplete [autocompletionOptions]="autocompletionOptions">
  2. </lib-ng-google-places-autocomplete>

See google.maps.places.AutocompleteRequest for valid options.

For example, to limit the results to Switzerland, you can specify the following configuration and pass it to the component:

  1. requestOptions: AutocompletionRequestOptions = {
  2. componentRestrictions: {country: 'CH'}
  3. };

Place details

An optional configuration can be passed to the component to configure the returned place result (when user selects a typeahead entry):

  1. <lib-ng-google-places-autocomplete [placesDetailsOptions]="placesDetailsOptions">
  2. </lib-ng-google-places-autocomplete>

See google.maps.places.PlaceDetailsRequest for valid options.

For example, to get longitude and latitude of a place include the geometry option:

  1. placesDetailsOptions: PlacesDetailsRequestOptions = {
  2. fields: ['geometry']
  3. };

Other attributes

[inputPlaceholder]: Specify a placeholder for the typeahead input field.
[(inputText)]: Model to bind the value in the input box to.

Compatibility

The library is compatible with Angular versions >=8.2.14 and ngx-bootstrap >=5.3.2.