@@ -23,7 +23,13 @@ declare global {
2323 }
2424}
2525
26- type Libraries = ( "drawing" | "geometry" | "localContext" | "places" | "visualization" ) [ ] ;
26+ type Libraries = (
27+ | "drawing"
28+ | "geometry"
29+ | "localContext"
30+ | "places"
31+ | "visualization"
32+ ) [ ] ;
2733
2834/**
2935 * The Google Maps JavaScript API
@@ -65,6 +71,10 @@ export interface LoaderOptions {
6571 * receive your default channel.
6672 */
6773 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 ;
6878 /**
6979 * When loading the Maps JavaScript API via the URL you may optionally load
7080 * additional libraries through use of the libraries URL parameter. Libraries
@@ -164,6 +174,10 @@ export class Loader {
164174 * See [[LoaderOptions.apiKey]]
165175 */
166176 apiKey : string ;
177+ /**
178+ * See [[LoaderOptions.id]]
179+ */
180+ id : string ;
167181 /**
168182 * See [[LoaderOptions.libraries]]
169183 */
@@ -205,6 +219,7 @@ export class Loader {
205219 */
206220 constructor ( {
207221 apiKey,
222+ id = "__googleMapsScriptId" ,
208223 libraries = [ ] ,
209224 language,
210225 region,
@@ -214,6 +229,7 @@ export class Loader {
214229 } : LoaderOptions ) {
215230 this . version = version ;
216231 this . apiKey = apiKey ;
232+ this . id = id ;
217233 this . libraries = libraries ;
218234 this . language = language ;
219235 this . region = region ;
@@ -293,9 +309,14 @@ export class Loader {
293309 * Set the script on document.
294310 */
295311 private setScript ( ) : void {
312+ if ( this . id && document . getElementById ( this . id ) ) {
313+ this . callback ( ) ;
314+ return ;
315+ }
316+
296317 const url = this . createUrl ( ) ;
297318 const script = document . createElement ( "script" ) ;
298-
319+ script . id = this . id ;
299320 script . type = "text/javascript" ;
300321 script . src = url ;
301322 script . onerror = this . loadErrorCallback . bind ( this ) ;
0 commit comments