View all files | ||||
Load the Google Maps JavaScript API script dynamically. This is an npm version of the Dynamic Library Import script.
Install @googlemaps/js-api-loader with:
TypeScript users should additionally install the types for the Google Maps JavaScript API:
If you use custom HTML elements from the Google Maps JavaScript API (e.g. <gmp-map>, and <gmp-advanced-marker>), you need to import them as well. Note that you do not need to await the result of importLibrary() in this case. The custom elements will upgrade automatically once the library is loaded and you can use the whenDefined() method to wait for the upgrade to complete.
This package exports just two functions, setOptions() and importLibrary().
Sets the options for loading the Google Maps JavaScript API and installs the global google.maps.importLibrary() function that is used by the importLibrary() function of this package.
This function should be called as early as possible in your application and should only be called once. Any further calls will not have any effect and log a warning to the console.
Below is a short summary of the accepted options, see the documentation for full descriptions and additional information:
Loads the specified library. Returns a promise that resolves with the library object when the library is loaded. In case of an error while loading the library (might be due to poor network conditions and other unforseeable circumstances), the promise is rejected with an error.
Calling this function for the first time will trigger loading the Google Maps JavaScript API itself.
The following libraries are available:
The loader supports pages that enforce require-trusted-types-for 'script'. When Trusted Types are available, the loader creates a policy named @googlemaps/js-api-loader and uses it to assign the Google Maps JavaScript API script URL.
If your page uses a trusted-types CSP directive, allow this policy name:
@googlemaps/js-api-loader is used by this package. google-maps-api-loader google-maps-api#html, and lit-html are used by the Maps JavaScript API script internally during execution.
If the policy name is not allowed, the loader logs a development warning and falls back to assigning a string URL. On pages that enforce require-trusted-types-for 'script', the browser will block that fallback.
See the migration guide.
Contributions are welcome and encouraged! If you'd like to contribute, send us a pull request and refer to our code of conduct and contributing guide.
This library uses Google Maps Platform services. Use of Google Maps Platform services through this library is subject to the Google Maps Platform Terms of Service.
This library is not a Google Maps Platform Core Service. Therefore, the Google Maps Platform Terms of Service (e.g. Technical Support Services, Service Level Agreements, and Deprecation Policy) do not apply to the code in this library.
If your billing address is in the European Economic Area, effective on 8 July 2025, the Google Maps Platform EEA Terms of Service will apply to your use of the Services. Functionality varies by region. Learn more.
This library is offered via an open source license. It is not governed by the Google Maps Platform Support Technical Support Services Guidelines, the SLA, or the Deprecation Policy. However, any Google Maps Platform services used by the library remain subject to the Google Maps Platform Terms of Service.
This library adheres to semantic versioning to indicate when backwards-incompatible changes are introduced.
If you find a bug, or have a feature request, please file an issue on GitHub. If you would like to get answers to technical questions from other Google Maps Platform developers, ask through one of our developer community channels. If you'd like to contribute, please check the contributing guide.
You can also discuss this library on our Discord server.