11
11
12
12
import { Directive , Input , NgZone , OnDestroy , OnInit , Output } from '@angular/core' ;
13
13
import { BehaviorSubject , Observable , Subject } from 'rxjs' ;
14
- import { map , take , takeUntil } from 'rxjs/operators' ;
14
+ import { takeUntil } from 'rxjs/operators' ;
15
15
16
16
import { GoogleMap } from '../google-map/google-map' ;
17
17
import { MapEventManager } from '../map-event-manager' ;
@@ -30,6 +30,9 @@ export class MapGroundOverlay implements OnInit, OnDestroy {
30
30
31
31
private readonly _opacity = new BehaviorSubject < number > ( 1 ) ;
32
32
private readonly _url = new BehaviorSubject < string > ( '' ) ;
33
+ private readonly _bounds =
34
+ new BehaviorSubject < google . maps . LatLngBounds | google . maps . LatLngBoundsLiteral | undefined > (
35
+ undefined ) ;
33
36
private readonly _destroyed = new Subject < void > ( ) ;
34
37
35
38
/**
@@ -46,7 +49,13 @@ export class MapGroundOverlay implements OnInit, OnDestroy {
46
49
}
47
50
48
51
/** Bounds for the overlay. */
49
- @Input ( ) bounds : google . maps . LatLngBounds | google . maps . LatLngBoundsLiteral ;
52
+ @Input ( )
53
+ get bounds ( ) : google . maps . LatLngBounds | google . maps . LatLngBoundsLiteral {
54
+ return this . _bounds . value ! ;
55
+ }
56
+ set bounds ( bounds : google . maps . LatLngBounds | google . maps . LatLngBoundsLiteral ) {
57
+ this . _bounds . next ( bounds ) ;
58
+ }
50
59
51
60
/** Whether the overlay is clickable */
52
61
@Input ( ) clickable : boolean = false ;
@@ -77,21 +86,30 @@ export class MapGroundOverlay implements OnInit, OnDestroy {
77
86
constructor ( private readonly _map : GoogleMap , private readonly _ngZone : NgZone ) { }
78
87
79
88
ngOnInit ( ) {
80
- if ( ! this . bounds && ( typeof ngDevMode === 'undefined' || ngDevMode ) ) {
81
- throw Error ( 'Image bounds are required' ) ;
82
- }
83
89
if ( this . _map . _isBrowser ) {
84
- this . _combineOptions ( ) . pipe ( take ( 1 ) ) . subscribe ( options => {
90
+ // The ground overlay setup is slightly different from the other Google Maps objects in that
91
+ // we have to recreate the `GroundOverlay` object whenever the bounds change, because
92
+ // Google Maps doesn't provide an API to update the bounds of an existing overlay.
93
+ this . _bounds . pipe ( takeUntil ( this . _destroyed ) ) . subscribe ( bounds => {
94
+ if ( this . groundOverlay ) {
95
+ this . groundOverlay . setMap ( null ) ;
96
+ this . groundOverlay = undefined ;
97
+ }
98
+
85
99
// Create the object outside the zone so its events don't trigger change detection.
86
100
// We'll bring it back in inside the `MapEventManager` only for the events that the
87
101
// user has subscribed to.
88
- this . _ngZone . runOutsideAngular ( ( ) => {
89
- this . groundOverlay =
90
- new google . maps . GroundOverlay ( this . _url . getValue ( ) , this . bounds , options ) ;
91
- } ) ;
92
- this . _assertInitialized ( ) ;
93
- this . groundOverlay . setMap ( this . _map . googleMap ! ) ;
94
- this . _eventManager . setTarget ( this . groundOverlay ) ;
102
+ if ( bounds ) {
103
+ this . _ngZone . runOutsideAngular ( ( ) => {
104
+ this . groundOverlay = new google . maps . GroundOverlay ( this . _url . getValue ( ) , bounds , {
105
+ clickable : this . clickable ,
106
+ opacity : this . _opacity . value ,
107
+ } ) ;
108
+ } ) ;
109
+ this . _assertInitialized ( ) ;
110
+ this . groundOverlay . setMap ( this . _map . googleMap ! ) ;
111
+ this . _eventManager . setTarget ( this . groundOverlay ) ;
112
+ }
95
113
} ) ;
96
114
97
115
this . _watchForOpacityChanges ( ) ;
@@ -138,19 +156,9 @@ export class MapGroundOverlay implements OnInit, OnDestroy {
138
156
return this . groundOverlay . getUrl ( ) ;
139
157
}
140
158
141
- private _combineOptions ( ) : Observable < google . maps . GroundOverlayOptions > {
142
- return this . _opacity . pipe ( map ( opacity => {
143
- const combinedOptions : google . maps . GroundOverlayOptions = {
144
- clickable : this . clickable ,
145
- opacity,
146
- } ;
147
- return combinedOptions ;
148
- } ) ) ;
149
- }
150
-
151
159
private _watchForOpacityChanges ( ) {
152
160
this . _opacity . pipe ( takeUntil ( this . _destroyed ) ) . subscribe ( opacity => {
153
- if ( opacity ) {
161
+ if ( opacity != null ) {
154
162
this . _assertInitialized ( ) ;
155
163
this . groundOverlay . setOpacity ( opacity ) ;
156
164
}
0 commit comments