-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgoogle-maps-provider.tsx
150 lines (131 loc) · 3.47 KB
/
google-maps-provider.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import React, {useState, useEffect, PropsWithChildren} from 'react';
import {GoogleMapsApiLoader} from './google-maps-api-loader';
// https://developers.google.com/maps/documentation/javascript/url-params
export interface GoogleMapsAPIUrlParameters {
googleMapsAPIKey: string;
libraries?: string[];
language?: string;
region?: string;
version?: string;
authReferrerPolicy?: string;
}
export interface GoogleMapsConfiguration {
mapContainer?: HTMLElement | null;
mapOptions?: google.maps.MapOptions;
}
export interface GoogleMapsProviderProps
extends GoogleMapsAPIUrlParameters,
GoogleMapsConfiguration {
onLoadScript?: () => void;
onLoadMap?: (map: google.maps.Map) => void;
}
export interface GoogleMapsContextType {
googleMapsAPIIsLoaded: boolean;
map?: google.maps.Map;
}
// Declare global maps callback function
declare global {
interface Window {
mapsCallback: () => void;
}
}
/**
* The Google Maps context
*/
export const GoogleMapsContext = React.createContext<GoogleMapsContextType>({
googleMapsAPIIsLoaded: false
});
const DEFAULT_LANGUAGE = navigator.language.slice(0, 2);
const DEFAULT_REGION = navigator.language.slice(3, 5);
/**
* The global Google Maps provider
*/
export const GoogleMapsProvider: React.FunctionComponent<
PropsWithChildren<GoogleMapsProviderProps>
> = props => {
const {
children,
googleMapsAPIKey,
mapContainer,
mapOptions,
libraries,
language,
region,
version,
authReferrerPolicy,
onLoadScript,
onLoadMap
} = props;
const [isLoadingAPI, setIsLoadingAPI] = useState<boolean>(true);
const [map, setMap] = useState<google.maps.Map>();
useEffect(() => {
console.log('effect: start loading');
const apiParams = {
key: googleMapsAPIKey,
language: language || DEFAULT_LANGUAGE,
region: region || DEFAULT_REGION,
libraries: libraries ? libraries.join(',') : undefined,
v: version,
authReferrerPolicy
};
setIsLoadingAPI(true);
GoogleMapsApiLoader.load(apiParams).then(
() => {
console.log('effect: loading done');
setIsLoadingAPI(false);
if (onLoadScript) {
onLoadScript();
}
},
err => {
console.error('effect: loading failed: ', err);
setIsLoadingAPI(false);
}
);
return () => {
console.log('effect/cleanup: unload API');
GoogleMapsApiLoader.unload();
};
}, [
googleMapsAPIKey,
JSON.stringify(libraries),
language,
region,
version,
authReferrerPolicy
]);
// Handle Google Maps map instance
useEffect(() => {
// Check for google.maps is needed because of Hot Module Replacement
if (
isLoadingAPI ||
!mapContainer ||
!(typeof google === 'object' && typeof google.maps === 'object')
) {
return () => {};
}
const newMap = new google.maps.Map(mapContainer, mapOptions);
google.maps.event.addListenerOnce(newMap, 'idle', () => {
if (onLoadMap && newMap) {
onLoadMap(newMap);
}
});
setMap(newMap);
// Remove all map related event listeners
return () => {
if (
newMap &&
typeof google === 'object' &&
typeof google.maps === 'object'
) {
google.maps.event.clearInstanceListeners(newMap);
}
};
}, [isLoadingAPI, mapContainer]);
return (
<GoogleMapsContext.Provider
value={{map, googleMapsAPIIsLoaded: !isLoadingAPI}}>
{children}
</GoogleMapsContext.Provider>
);
};