Skip to content

Commit 4e7ed85

Browse files
committed
docs: update documentation regarding library loading
1 parent 2e23cd8 commit 4e7ed85

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

MIGRATION.md

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ functional approach. This simplifies the API and aligns it with
1010
[the recommended way](https://developers.google.com/maps/documentation/javascript/load-maps-js-api)
1111
to load the Google Maps JavaScript API.
1212

13-
- **v1.x:** You would create an instance of the `Loader` class with your
14-
configuration and then call methods like `load()` or `importLibrary()` on
13+
- **v1.x:** You would create an instance of the `Loader` class with your
14+
configuration and then call methods like `load()` or `importLibrary()` on
1515
that instance.
16-
- **v2.x:** You now use two standalone functions: `setOptions()` to
16+
- **v2.x:** You now use two standalone functions: `setOptions()` to
1717
configure the API loader and `importLibrary()` to load specific libraries.
1818

1919
Generally, the Loader constructor can be replaced with a call to `setOptions()`,
@@ -22,13 +22,13 @@ with calls to `importLibrary()`.
2222

2323
## Key Changes
2424

25-
| Feature | v1.x (`Loader` class) | v2.x (functions) |
26-
| :--------------------- | :----------------------------------- | :-------------------------------------- |
27-
| **Initialization** | `new Loader({ apiKey: '...', ... })` | `setOptions({ key: '...', ... })` |
28-
| **Loading Libraries** | `loader.importLibrary('maps')` | `importLibrary('maps')` |
29-
| **Legacy Loading** | `loader.load()` | Removed. Use `importLibrary()` instead. |
30-
| **API Key Parameters** | `apiKey` | `key` |
31-
| | `version` | `v` |
25+
| Feature | v1.x (`Loader` class) | v2.x (functions) |
26+
| :--------------------- | :--------------------------------------------------- | :-------------------------------------- |
27+
| **Initialization** | `new Loader({ apiKey: '...', ... })` | `setOptions({ key: '...', ... })` |
28+
| **Loading Libraries** | option `libraries` or `loader.importLibrary('maps')` | `importLibrary('maps')` |
29+
| **Legacy Loading** | `loader.load()` | Removed. Use `importLibrary()` instead. |
30+
| **API Key Parameters** | `apiKey` | `key` |
31+
| | `version` | `v` |
3232

3333
## Typical Use Cases Compared
3434

@@ -70,7 +70,8 @@ function initMap() {
7070
### v2.x
7171

7272
The typical use case from versions 2.0 onwards is to use `importLibrary` to get
73-
access to the classes and features needed from the Maps JavaScript API.
73+
access to the classes and features needed from the Maps JavaScript API,
74+
when they are actually needed.
7475

7576
```javascript
7677
import { setOptions, importLibrary } from "@googlemaps/js-api-loader";
@@ -82,6 +83,7 @@ setOptions({
8283

8384
try {
8485
const { Map } = await importLibrary("maps");
86+
8587
// Use the maps library
8688
const map = new Map(document.getElementById("map"), {
8789
center: { lat: -34.397, lng: 150.644 },
@@ -92,7 +94,7 @@ try {
9294
}
9395
```
9496

95-
However, all the examples from the 1.x version can also be written based
97+
However, all the examples from the 1.x version can still be written based
9698
on the 2.x version, since – besides returning the library object – the
9799
`importLibrary()` function also populates the global `google.maps` namespace.
98100

@@ -106,24 +108,27 @@ setOptions({
106108
key: "YOUR_API_KEY",
107109
v: "weekly",
108110

109-
// Libraries can still be specified in `setOptions`. This makes sure that
110-
// all libraries are available when the importLibrary promise is resolved.
111-
libraries: ["maps", "places"],
111+
// Libraries can still be specified in `setOptions`, but this is only
112+
// preloading them. You still need to call `importLibrary` to fully load
113+
// them.
114+
libraries: ["places"],
112115
});
113116

117+
// load all required libraries in parallel
118+
const librariesPromise = Promise.all([
119+
importLibrary("maps"),
120+
importLibrary("places"),
121+
]);
122+
114123
// The examples from above, rewritten with v2.0:
115124
//
116-
// a) using promises (note: which library is imported in these cases makes
117-
// little difference: the libraries were specified in `setOptions` and
118-
// we're not using the returned value)
119-
importLibrary("core").then(() => initMap());
125+
// a) using promises and c) using callbacks
126+
librariesPromise.then(() => initMap());
120127

121128
// b) using load() with async/await:
122-
await importLibrary("core");
129+
await librariesPromise;
123130
initMap();
124131

125-
// c) using a callback – this is identical to a)
126-
127132
function initMap() {
128133
// Use the global google.maps namespace once loading is complete
129134
const map = new google.maps.Map(document.getElementById("map"), {

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Below is a short summary of the accepted options, see the
118118
- `v: string`: The version of the Maps JavaScript API to load.
119119
- `language: string`: The language to use.
120120
- `region: string`: The region code to use.
121-
- `libraries: string[]`: Additional libraries to load.
121+
- `libraries: string[]`: Additional libraries to preload.
122122
- `authReferrerPolicy: string`: Set the referrer policy for the API requests.
123123
- `mapIds: string[]`: An array of map IDs to preload.
124124
- `channel: string`: Can be used to track your usage.

0 commit comments

Comments
 (0)