New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ngx-google-maps-places-api

Package Overview
Dependencies
Maintainers
0
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ngx-google-maps-places-api

Angular library that provides a seamless integration with new Google Maps Places API, offering easy-to-use services for place details retrieval and management in Angular applications.

  • 19.0.0
  • latest
  • stable
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
122
decreased by-50.2%
Maintainers
0
Weekly downloads
 
Created
Source

Angular Google Maps Places API

Angular Google Maps Places Logo
Angular library that provides a seamless integration with new Google Maps Places API, offering easy-to-use services for place details retrieval and management in Angular applications.
Compatible with Angular **>= 18.x.x**. See Versioning.

Demo

Build Publish npm version npm

Example


Features

  • Autocomplete suggestions for places
  • Place details fetching
  • Address component parsing

Installation

npm install --save ngx-google-maps-places-api

Getting the API Key

Follow these steps to get an API key that can be used to load Google Maps.

Loading the API

Include the Dynamic Library Import script in the index.html of your app. When a Google Map is being rendered, it'll use the Dynamic Import API to load the necessary JavaScript automatically.

<!-- index.html -->
<!DOCTYPE html>
<body>
  ...
  <script>
    (g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({
      v: "weekly",
      key: YOUR_API_KEY_GOES_HERE
    });
  </script>
</body>
</html>

Note: the component also supports loading the API using the legacy script tag, however it isn't recommended because it requires all of the Google Maps JavaScript to be loaded up-front, even if it isn't used.

Usage

Inject the NgxGoogleMapsPlacesApiService in your Angular component/directive/etc:

import { Directive, inject } from '@angular/core';
import { NgxGoogleMapsPlacesApiService } from 'ngx-google-maps-places-api';

@Directive({
  selector: 'app-test',
  standalone: true
})
export class AppTestDirective {
  private readonly _ngxGoogleMapsPlacesApiService = inject(NgxGoogleMapsPlacesApiService);
  // Use the service methods
}

Versioning

Library tested for Angular versions >= 18.x.x.

Versions are consistent with major Angular version. E.g.:

Use v18.x.x with Angular 18.x.x.

Dependencies

  • Angular
  • RxJs

API reference

NgxGoogleMapsPlacesApiService

Methods

  • refreshToken
    Refreshes the Google Maps Places autocomplete session token. The session token is used to associate autocomplete suggestions with a particular session. This method creates a new session token and stores it in the service.

  • fetchSuggestions
    Fetches autocomplete suggestions from the Google Maps Places API based on the provided request or input string.

NameDescription
Parameters
requestOrInput: google.maps.places.AutocompleteRequest | stringThe autocomplete request object or an input string to fetch suggestions for.
Returns
Observable<google.maps.places.AutocompleteSuggestion[]>An observable that emits an array of autocomplete suggestions.
  • fetchPlaceDetails
    Fetches the details of a Google Maps place based on an autocomplete suggestion or place prediction.
NameDescription
Parameters
suggestionOrPrediction: google.maps.places.AutocompleteSuggestion | google.maps.places.PlacePredictionThe autocomplete suggestion or place prediction to fetch details for.
fields: string[]An optional array of fields to fetch for the place. Defaults to [ 'addressComponents' ].
Returns
Observable<google.maps.places.Place | null>An observable that emits the fetched place details, or null if the place prediction is invalid.
  • parseAddressComponents
    Parses the address components of a Google Maps place and returns an AddressModel object.
NameDescription
Parameters
place: google.maps.places.Place | nullThe Google Maps place object to parse the address components from.
Returns
AddressModel | nullAn AddressModel object containing the parsed address components, or null if the place is null or has no address components.
  • applyBoldToMatches
    Applies bold formatting to the matched text within the provided FormattableText (see FormattableText)..
NameDescription
Parameters
formattableText: google.maps.places.FormattableText | nullThe FormattableText object containing the text and match information.
Returns****
stringThe formatted text with the matched portions wrapped in <b> tags.

Build

Run ng build ngx-google-maps-places-api to build the project. The build artifacts will be stored in the dist/ directory.

Running unit tests

Run ng test ngx-google-maps-places-api to execute the unit tests via Jest.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Keywords

FAQs

Package last updated on 22 Nov 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc