@@ -190,7 +190,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
190
190
@Output ( ) zoomChanged = new EventEmitter < void > ( ) ;
191
191
192
192
private _mapEl : HTMLElement ;
193
- _googleMap ! : UpdatedGoogleMap ;
193
+ _googleMap : UpdatedGoogleMap ;
194
194
195
195
/** Whether we're currently rendering inside a browser. */
196
196
private _isBrowser : boolean ;
@@ -244,8 +244,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
244
244
this . _googleMapChanges = this . _initializeMap ( combinedOptionsChanges ) ;
245
245
this . _googleMapChanges . subscribe ( ( googleMap : google . maps . Map ) => {
246
246
this . _googleMap = googleMap as UpdatedGoogleMap ;
247
-
248
- this . _initializeEventHandlers ( ) ;
247
+ this . _initializeEventHandlers ( this . _googleMap ) ;
249
248
} ) ;
250
249
251
250
this . _watchForOptionsChanges ( ) ;
@@ -267,6 +266,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
267
266
fitBounds (
268
267
bounds : google . maps . LatLngBounds | google . maps . LatLngBoundsLiteral ,
269
268
padding ?: number | google . maps . Padding ) {
269
+ this . _assertInitialized ( ) ;
270
270
this . _googleMap . fitBounds ( bounds , padding ) ;
271
271
}
272
272
@@ -275,6 +275,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
275
275
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.panBy
276
276
*/
277
277
panBy ( x : number , y : number ) {
278
+ this . _assertInitialized ( ) ;
278
279
this . _googleMap . panBy ( x , y ) ;
279
280
}
280
281
@@ -283,6 +284,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
283
284
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.panTo
284
285
*/
285
286
panTo ( latLng : google . maps . LatLng | google . maps . LatLngLiteral ) {
287
+ this . _assertInitialized ( ) ;
286
288
this . _googleMap . panTo ( latLng ) ;
287
289
}
288
290
@@ -293,6 +295,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
293
295
panToBounds (
294
296
latLngBounds : google . maps . LatLngBounds | google . maps . LatLngBoundsLiteral ,
295
297
padding ?: number | google . maps . Padding ) {
298
+ this . _assertInitialized ( ) ;
296
299
this . _googleMap . panToBounds ( latLngBounds , padding ) ;
297
300
}
298
301
@@ -301,6 +304,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
301
304
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.getBounds
302
305
*/
303
306
getBounds ( ) : google . maps . LatLngBounds | null {
307
+ this . _assertInitialized ( ) ;
304
308
return this . _googleMap . getBounds ( ) || null ;
305
309
}
306
310
@@ -309,6 +313,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
309
313
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.getCenter
310
314
*/
311
315
getCenter ( ) : google . maps . LatLng {
316
+ this . _assertInitialized ( ) ;
312
317
return this . _googleMap . getCenter ( ) ;
313
318
}
314
319
@@ -317,6 +322,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
317
322
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.getClickableIcons
318
323
*/
319
324
getClickableIcons ( ) : boolean {
325
+ this . _assertInitialized ( ) ;
320
326
return this . _googleMap . getClickableIcons ( ) ;
321
327
}
322
328
@@ -325,6 +331,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
325
331
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.getHeading
326
332
*/
327
333
getHeading ( ) : number {
334
+ this . _assertInitialized ( ) ;
328
335
return this . _googleMap . getHeading ( ) ;
329
336
}
330
337
@@ -333,6 +340,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
333
340
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.getMapTypeId
334
341
*/
335
342
getMapTypeId ( ) : google . maps . MapTypeId | string {
343
+ this . _assertInitialized ( ) ;
336
344
return this . _googleMap . getMapTypeId ( ) ;
337
345
}
338
346
@@ -341,6 +349,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
341
349
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.getProjection
342
350
*/
343
351
getProjection ( ) : google . maps . Projection | null {
352
+ this . _assertInitialized ( ) ;
344
353
return this . _googleMap . getProjection ( ) ;
345
354
}
346
355
@@ -349,6 +358,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
349
358
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.getStreetView
350
359
*/
351
360
getStreetView ( ) : google . maps . StreetViewPanorama {
361
+ this . _assertInitialized ( ) ;
352
362
return this . _googleMap . getStreetView ( ) ;
353
363
}
354
364
@@ -357,6 +367,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
357
367
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.getTilt
358
368
*/
359
369
getTilt ( ) : number {
370
+ this . _assertInitialized ( ) ;
360
371
return this . _googleMap . getTilt ( ) ;
361
372
}
362
373
@@ -365,6 +376,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
365
376
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.getZoom
366
377
*/
367
378
getZoom ( ) : number {
379
+ this . _assertInitialized ( ) ;
368
380
return this . _googleMap . getZoom ( ) ;
369
381
}
370
382
@@ -373,6 +385,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
373
385
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.controls
374
386
*/
375
387
get controls ( ) : Array < google . maps . MVCArray < Node > > {
388
+ this . _assertInitialized ( ) ;
376
389
return this . _googleMap . controls ;
377
390
}
378
391
@@ -381,6 +394,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
381
394
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.data
382
395
*/
383
396
get data ( ) : google . maps . Data {
397
+ this . _assertInitialized ( ) ;
384
398
return this . _googleMap . data ;
385
399
}
386
400
@@ -389,6 +403,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
389
403
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.mapTypes
390
404
*/
391
405
get mapTypes ( ) : google . maps . MapTypeRegistry {
406
+ this . _assertInitialized ( ) ;
392
407
return this . _googleMap . mapTypes ;
393
408
}
394
409
@@ -397,6 +412,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
397
412
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.overlayMapTypes
398
413
*/
399
414
get overlayMapTypes ( ) : google . maps . MVCArray < google . maps . MapType > {
415
+ this . _assertInitialized ( ) ;
400
416
return this . _googleMap . overlayMapTypes ;
401
417
}
402
418
@@ -423,9 +439,8 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
423
439
private _initializeMap ( optionsChanges : Observable < google . maps . MapOptions > ) :
424
440
Observable < google . maps . Map > {
425
441
return optionsChanges . pipe (
426
- take ( 1 ) , map ( options => {
427
- return new google . maps . Map ( this . _mapEl , options ) ;
428
- } ) ,
442
+ take ( 1 ) ,
443
+ map ( options => new google . maps . Map ( this . _mapEl , options ) ) ,
429
444
shareReplay ( 1 ) ) ;
430
445
}
431
446
@@ -457,7 +472,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
457
472
} ) ;
458
473
}
459
474
460
- private _initializeEventHandlers ( ) {
475
+ private _initializeEventHandlers ( googleMap : UpdatedGoogleMap ) {
461
476
// Ensure that we don't leak if called multiple times.
462
477
this . _clearListeners ( ) ;
463
478
@@ -484,7 +499,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
484
499
] ) ;
485
500
eventHandlers . forEach ( ( eventHandler : EventEmitter < void > , name : string ) => {
486
501
if ( eventHandler . observers . length > 0 ) {
487
- this . _listeners . push ( this . _googleMap . addListener ( name , ( ) => {
502
+ this . _listeners . push ( googleMap . addListener ( name , ( ) => {
488
503
eventHandler . emit ( ) ;
489
504
} ) ) ;
490
505
}
@@ -493,13 +508,13 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
493
508
( eventHandler : EventEmitter < google . maps . MouseEvent > , name : string ) => {
494
509
if ( eventHandler . observers . length > 0 ) {
495
510
this . _listeners . push (
496
- this . _googleMap . addListener ( name , ( event : google . maps . MouseEvent ) => {
511
+ googleMap . addListener ( name , ( event : google . maps . MouseEvent ) => {
497
512
eventHandler . emit ( event ) ;
498
513
} ) ) ;
499
514
}
500
515
} ) ;
501
516
if ( this . mapClick . observers . length > 0 ) {
502
- this . _listeners . push ( this . _googleMap . addListener (
517
+ this . _listeners . push ( googleMap . addListener (
503
518
'click' , ( event : google . maps . MouseEvent | google . maps . IconMouseEvent ) => {
504
519
this . mapClick . emit ( event ) ;
505
520
} ) ) ;
@@ -514,4 +529,11 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
514
529
515
530
this . _listeners = [ ] ;
516
531
}
532
+
533
+ private _assertInitialized ( ) {
534
+ if ( ! this . _googleMap ) {
535
+ throw Error ( 'Cannot access Google Map information before the API has been initialized. ' +
536
+ 'Please wait for the API to load before trying to interact with it.' ) ;
537
+ }
538
+ }
517
539
}
0 commit comments