@@ -23,7 +23,13 @@ declare global {
23
23
}
24
24
}
25
25
26
- type Libraries = ( "drawing" | "geometry" | "localContext" | "places" | "visualization" ) [ ] ;
26
+ type Libraries = (
27
+ | "drawing"
28
+ | "geometry"
29
+ | "localContext"
30
+ | "places"
31
+ | "visualization"
32
+ ) [ ] ;
27
33
28
34
/**
29
35
* The Google Maps JavaScript API
@@ -65,6 +71,10 @@ export interface LoaderOptions {
65
71
* receive your default channel.
66
72
*/
67
73
version ?: string ;
74
+ /**
75
+ * The id of the script tag. Before adding a new script, the Loader will check for an existing one.
76
+ */
77
+ id ?: string ;
68
78
/**
69
79
* When loading the Maps JavaScript API via the URL you may optionally load
70
80
* additional libraries through use of the libraries URL parameter. Libraries
@@ -164,6 +174,10 @@ export class Loader {
164
174
* See [[LoaderOptions.apiKey]]
165
175
*/
166
176
apiKey : string ;
177
+ /**
178
+ * See [[LoaderOptions.id]]
179
+ */
180
+ id : string ;
167
181
/**
168
182
* See [[LoaderOptions.libraries]]
169
183
*/
@@ -205,6 +219,7 @@ export class Loader {
205
219
*/
206
220
constructor ( {
207
221
apiKey,
222
+ id = "__googleMapsScriptId" ,
208
223
libraries = [ ] ,
209
224
language,
210
225
region,
@@ -214,6 +229,7 @@ export class Loader {
214
229
} : LoaderOptions ) {
215
230
this . version = version ;
216
231
this . apiKey = apiKey ;
232
+ this . id = id ;
217
233
this . libraries = libraries ;
218
234
this . language = language ;
219
235
this . region = region ;
@@ -293,9 +309,14 @@ export class Loader {
293
309
* Set the script on document.
294
310
*/
295
311
private setScript ( ) : void {
312
+ if ( this . id && document . getElementById ( this . id ) ) {
313
+ this . callback ( ) ;
314
+ return ;
315
+ }
316
+
296
317
const url = this . createUrl ( ) ;
297
318
const script = document . createElement ( "script" ) ;
298
-
319
+ script . id = this . id ;
299
320
script . type = "text/javascript" ;
300
321
script . src = url ;
301
322
script . onerror = this . loadErrorCallback . bind ( this ) ;
0 commit comments