@@ -28,6 +28,7 @@ export class MapGroundOverlay implements OnInit, OnDestroy {
28
28
private _eventManager = new MapEventManager ( this . _ngZone ) ;
29
29
30
30
private readonly _opacity = new BehaviorSubject < number > ( 1 ) ;
31
+ private readonly _url = new BehaviorSubject < string > ( '' ) ;
31
32
private readonly _destroyed = new Subject < void > ( ) ;
32
33
33
34
/**
@@ -37,13 +38,19 @@ export class MapGroundOverlay implements OnInit, OnDestroy {
37
38
*/
38
39
groundOverlay ?: google . maps . GroundOverlay ;
39
40
40
- @Input ( ) url ! : string ; // Asserted in ngOnInit.
41
-
41
+ /** URL of the image that will be shown in the overlay. */
42
42
@Input ( )
43
- bounds ! : google . maps . LatLngBounds | google . maps . LatLngBoundsLiteral ; // Asserted in ngOnInit.
43
+ set url ( url : string ) {
44
+ this . _url . next ( url ) ;
45
+ }
44
46
45
- @Input ( ) clickable = false ;
47
+ /** Bounds for the overlay. */
48
+ @Input ( ) bounds : google . maps . LatLngBounds | google . maps . LatLngBoundsLiteral ;
46
49
50
+ /** Whether the overlay is clickable */
51
+ @Input ( ) clickable : boolean = false ;
52
+
53
+ /** Opacity of the overlay. */
47
54
@Input ( )
48
55
set opacity ( opacity : number ) {
49
56
this . _opacity . next ( opacity ) ;
@@ -69,9 +76,6 @@ export class MapGroundOverlay implements OnInit, OnDestroy {
69
76
constructor ( private readonly _map : GoogleMap , private readonly _ngZone : NgZone ) { }
70
77
71
78
ngOnInit ( ) {
72
- if ( ! this . url ) {
73
- throw Error ( 'An image url is required' ) ;
74
- }
75
79
if ( ! this . bounds ) {
76
80
throw Error ( 'Image bounds are required' ) ;
77
81
}
@@ -81,14 +85,16 @@ export class MapGroundOverlay implements OnInit, OnDestroy {
81
85
// We'll bring it back in inside the `MapEventManager` only for the events that the
82
86
// user has subscribed to.
83
87
this . _ngZone . runOutsideAngular ( ( ) => {
84
- this . groundOverlay = new google . maps . GroundOverlay ( this . url , this . bounds , options ) ;
88
+ this . groundOverlay =
89
+ new google . maps . GroundOverlay ( this . _url . getValue ( ) , this . bounds , options ) ;
85
90
} ) ;
86
91
this . _assertInitialized ( ) ;
87
92
this . groundOverlay . setMap ( this . _map . googleMap ! ) ;
88
93
this . _eventManager . setTarget ( this . groundOverlay ) ;
89
94
} ) ;
90
95
91
96
this . _watchForOpacityChanges ( ) ;
97
+ this . _watchForUrlChanges ( ) ;
92
98
}
93
99
}
94
100
@@ -150,6 +156,18 @@ export class MapGroundOverlay implements OnInit, OnDestroy {
150
156
} ) ;
151
157
}
152
158
159
+ private _watchForUrlChanges ( ) {
160
+ this . _url . pipe ( takeUntil ( this . _destroyed ) ) . subscribe ( url => {
161
+ this . _assertInitialized ( ) ;
162
+ const overlay = this . groundOverlay ;
163
+ overlay . set ( 'url' , url ) ;
164
+
165
+ // Google Maps only redraws the overlay if we re-set the map.
166
+ overlay . setMap ( null ) ;
167
+ overlay . setMap ( this . _map . googleMap ! ) ;
168
+ } ) ;
169
+ }
170
+
153
171
private _assertInitialized ( ) : asserts this is { groundOverlay : google . maps . GroundOverlay } {
154
172
if ( ! this . _map . googleMap ) {
155
173
throw Error (
0 commit comments