Skip to content

Commit 9607ca4

Browse files
authored
Versioning (#35)
* feat: add version option * fix: readme typo
1 parent 1af3926 commit 9607ca4

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ interface GoogleMapProviderProps {
122122
// Use this parameter for cloud-based map styling (in beta), see: https://developers.google.com/maps/documentation/javascript/cloud-based-map-styling
123123
mapIds?: string[];
124124

125+
// Use this parameter to specify a version, see: https://developers.google.com/maps/documentation/javascript/versions
126+
version?: string;
127+
125128
// A callback function that is called, when the map is loaded.
126129
onLoad?: (map: google.maps.Map) => void;
127130
}

src/google-map.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ interface GoogleMapOptions {
1111
language?: string;
1212
region?: string;
1313
mapIds?: string[];
14+
version?: string;
1415
}
1516

1617
/**
@@ -35,19 +36,30 @@ export default class GoogleMap {
3536
const scriptTag = document.createElement('script');
3637
const defaultLanguage = window.navigator.language.slice(0, 2);
3738
const defaultRegion = window.navigator.language.slice(3, 5);
39+
40+
const {
41+
libraries,
42+
mapIds,
43+
version,
44+
language,
45+
region,
46+
googleMapsAPIKey,
47+
onLoadScript
48+
} = options;
49+
3850
scriptTag.setAttribute('type', 'text/javascript');
3951
scriptTag.setAttribute(
4052
'src',
41-
`https://maps.googleapis.com/maps/api/js?key=${
42-
options.googleMapsAPIKey
43-
}&language=${options.language || defaultLanguage}&region=${options.region || defaultRegion}${
44-
options.libraries ? `&libraries=${options.libraries.join(',')}` : ''
45-
}${
46-
options.mapIds ? `&map_ids=${options.mapIds.join(',')}` : ''
53+
`https://maps.googleapis.com/maps/api/js?key=${googleMapsAPIKey}&language=${
54+
language || defaultLanguage
55+
}&region=${region || defaultRegion}${
56+
libraries ? `&libraries=${libraries.join(',')}` : ''
57+
}${mapIds ? `&map_ids=${mapIds.join(',')}` : ''}${
58+
version ? `&v=${version}` : ''
4759
}`
4860
);
4961
scriptTag.onload = (): void => {
50-
options.onLoadScript();
62+
onLoadScript();
5163
this.initMap(options);
5264
};
5365
document.getElementsByTagName('head')[0].appendChild(scriptTag);
@@ -81,9 +93,11 @@ export default class GoogleMap {
8193
*/
8294
public destroyComplete = (): void => {
8395
if (this.map) {
84-
document.querySelectorAll('script[src^="https://maps.googleapis.com"]').forEach(script => {
85-
script.remove();
86-
});
96+
document
97+
.querySelectorAll('script[src^="https://maps.googleapis.com"]')
98+
.forEach((script) => {
99+
script.remove();
100+
});
87101
if (window.google && window.google.maps) {
88102
// @ts-ignore: The operand of a 'delete' operator must be optional.
89103
delete window.google.maps;

src/map-provider.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface GoogleMapProviderProps {
1010
language?: string;
1111
region?: string;
1212
mapIds?: string[];
13+
version?: string;
1314
onLoad?: (map: google.maps.Map) => void;
1415
}
1516

@@ -39,6 +40,7 @@ const GoogleMapProvider: React.FunctionComponent<GoogleMapProviderProps> = (
3940
language,
4041
region,
4142
mapIds,
43+
version,
4244
onLoad
4345
} = props;
4446

@@ -64,7 +66,8 @@ const GoogleMapProvider: React.FunctionComponent<GoogleMapProviderProps> = (
6466
libraries,
6567
mapIds,
6668
language,
67-
region
69+
region,
70+
version
6871
};
6972
// Create Google Map instance
7073
new GoogleMap(mapOptions);

0 commit comments

Comments
 (0)