Skip to content

Commit 1718a4a

Browse files
Added all google maps api libraries to api loader (google-map-react#921)
* Added all google maps api libraries to api loader Supports places, visualization, places, and geomerty libs. keeps support for previous heatMapLibrary prop to avoid breaking older usage. * add libraries to props of GoogleMap * Update google_map.js * add warning about heatMapLibrary * heatmap depreaction, clean libraries, docs * clean up google map prop
1 parent d3f258d commit 1718a4a

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

API.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Example:
1616
key: API_KEY,
1717
language: 'ru',
1818
region: 'ru',
19+
libraries:['places'],
1920
...otherUrlParams,
2021
}}
2122
>
@@ -350,7 +351,7 @@ For more details see the [google documentation](https://developers.google.com/ma
350351

351352
### Heatmap Layer
352353

353-
To use the heatmap layer, add `heatmapLibrary={true}` to add the visualizations library, and provide the data&configuration for the heatmap in `heatmap` as props.
354+
To use the heatmap layer, add `visualization` to the libraries property array on `bootstrapURLKeys` and provide the data & configuration for the heatmap in `heatmap` as props.
354355

355356
The typescript interface for the heatmap prop is as follows:
356357
```typescript
@@ -372,10 +373,12 @@ interface heatmapProp {
372373

373374
```javascript
374375
<GoogleMapReact
375-
bootstrapURLKeys={{ key: [YOUR_KEY] }}
376+
bootstrapURLKeys={{
377+
key: [YOUR_KEY],
378+
libraries:['visualization']
379+
}}
376380
zoom={zoom}
377381
center={center}
378-
heatmapLibrary={true}
379382
heatmap={{data}}
380383
>
381384
{markers}
@@ -384,8 +387,24 @@ interface heatmapProp {
384387

385388
#### Important Note
386389

387-
If you have multiple `GoogleMapReact` components in project and you want to use heatmap layer so provide `heatmapLibrary={true}` for all `GoogleMapReact` components so component will load heatmap library at the beginning with google map api.
390+
If you have multiple `GoogleMapReact` components in project and you want to use heatmap layer provide `libraries:['visualization']` to `bootstrapURLKeys` so that the visualization library will be loaded with the google map api.
388391

389392
### Localizing the Map
390393

391394
This is done by setting bootstrapURLKeys.[language](https://developers.google.com/maps/documentation/javascript/localization#Language) and bootstrapURLKeys.[region](https://developers.google.com/maps/documentation/javascript/localization#Region). Also notice that setting region to 'cn' is required when using the map from within China, see [google documentation](https://developers.google.com/maps/documentation/javascript/localization#GoogleMapsChina) for more info. Setting 'cn' will result in use of the specific API URL for China.
395+
396+
397+
### Libraries
398+
399+
If you want to include additional libraries to load with the maps api, indicate them in the libraries property of the `bootstrapURLKeys` object.
400+
401+
Example:
402+
```JSX
403+
<GoogleMapReact
404+
bootstrapURLKeys={{
405+
key: [YOUR_KEY],
406+
libraries:['places', 'geometry', 'drawing', 'visualization']
407+
}}
408+
{markers}
409+
</GoogleMapReact>
410+
```

src/loaders/google_map_loader.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,31 @@ export default (bootstrapURLKeys, heatmapLibrary) => {
5858
}
5959
}
6060

61+
// Support for older version using heatMapLibrary option
62+
if (heatMapLibrary) {
63+
bootstrapURLKeys.libraries
64+
? bootstrapURLKeys.libraries.append('visualization')
65+
: (bootstrapURLKeys['libraries'] = ['visualization']);
66+
console.warn(
67+
"heatMapLibrary will be deprecated in the future. Please use bootstrapURLKeys.libraries property instead (libraries=['visualization'])."
68+
);
69+
}
70+
71+
// clean unknown and remove duplicates
72+
const googleMapsLibs = ['places', 'drawing', 'geometry', 'visualization'];
73+
if (bootstrapURLKeys.libraries) {
74+
bootstrapURLKeys.libraries = bootstrapURLKeys.libraries.filter(
75+
(lib, i) =>
76+
bootstrapURLKeys.libraries.indexOf(lib) === i &&
77+
googleMapsLibs.includes(lib)
78+
);
79+
}
80+
6181
const params = Object.keys(bootstrapURLKeys).reduce(
6282
(r, key) => `${r}&${key}=${bootstrapURLKeys[key]}`,
6383
''
6484
);
65-
66-
const libraries = heatmapLibrary ? '&libraries=visualization' : '';
67-
85+
6886
$script_(
6987
`${DEFAULT_URL}${API_PATH}${params}${libraries}`,
7088
() =>

0 commit comments

Comments
 (0)