@@ -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 )
1111to 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
1919Generally, 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
7272The 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
7677import { setOptions , importLibrary } from " @googlemaps/js-api-loader" ;
@@ -82,6 +83,7 @@ setOptions({
8283
8384try {
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 },
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
9698on 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 ;
123130initMap ();
124131
125- // c) using a callback – this is identical to a)
126-
127132function initMap () {
128133 // Use the global google.maps namespace once loading is complete
129134 const map = new google.maps.Map (document .getElementById (" map" ), {
0 commit comments