JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 32017
  • Score
    100M100P100Q147921F
  • License MIT

A thin, Promise-returning helper for loading the Google Maps JavaScript API.

Package Exports

  • load-google-maps-api

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (load-google-maps-api) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

load-google-maps-api npm Version Build Status

A thin, Promise-returning helper for loading the Google Maps JavaScript API.

  • The Promise’s fulfilled callback is passed the google.maps object
  • Optionally set a timeout, an API key, the language, and more
  • 414 bytes gzipped

Usage

Editable demo (CodePen)

const loadGoogleMapsApi = require('load-google-maps-api')

loadGoogleMapsApi().then(function (googleMaps) {
  new googleMaps.Map(document.querySelector('.map'), {
    center: {
      lat: 40.7484405,
      lng: -73.9944191
    },
    zoom: 12
  })
}).catch(function (error) {
  console.error(error)
})

N.B. Just like the Google Maps API itself, this module is client-side only.

Why

Without this module, you would need to specify a named global callback, and pass said callback’s name as a parameter in the script tag’s src. For example:

<script>
window.googleMapsOnLoad = function () {
  // `window.google.maps` available here
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?callback=googleMapsOnLoad"></script>

This module abstracts this ceremony away, and fits better with modern bundlers like Browserify or Webpack.

API

const loadGoogleMapsApi = require('load-google-maps-api')

loadGoogleMapsApi([options])

Returns a Promise.

  • Fulfilled if loading was successful. The fulfilled callback is passed the google.maps object.
  • Rejected if we weren’t able to load the Google Maps API after options.timeout.

See Usage.

options is an optional object literal:

Key Description Default
channel Client usage reporting channel undefined
client Client ID undefined
key Your API key undefined
language Language undefined
libraries Supplemental libraries to load []
region Region undefined
timeout Time in milliseconds before rejecting the Promise 10000
v API version undefined

Installation

Install via yarn:

$ yarn add load-google-maps-api

Or npm:

$ npm install --save load-google-maps-api

License

MIT