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,14 +49,21 @@ 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 ;
53
62
54
63
/** Opacity of the overlay. */
55
64
@Input ( )
56
65
set opacity ( opacity : number ) {
66
+ console . log ( 'received' , opacity ) ;
57
67
this . _opacity . next ( opacity ) ;
58
68
}
59
69
@@ -77,21 +87,30 @@ export class MapGroundOverlay implements OnInit, OnDestroy {
77
87
constructor ( private readonly _map : GoogleMap , private readonly _ngZone : NgZone ) { }
78
88
79
89
ngOnInit ( ) {
80
- if ( ! this . bounds && ( typeof ngDevMode === 'undefined' || ngDevMode ) ) {
81
- throw Error ( 'Image bounds are required' ) ;
82
- }
83
90
if ( this . _map . _isBrowser ) {
84
- this . _combineOptions ( ) . pipe ( take ( 1 ) ) . subscribe ( options => {
91
+ // The ground overlay setup is slightly different from the other Google Maps objects in that
92
+ // we have to recreate the `GroundOverlay` object whenever the bounds change, because
93
+ // Google Maps doesn't provide an API to update the bounds of an existing overlay.
94
+ this . _bounds . pipe ( takeUntil ( this . _destroyed ) ) . subscribe ( bounds => {
95
+ if ( this . groundOverlay ) {
96
+ this . groundOverlay . setMap ( null ) ;
97
+ this . groundOverlay = undefined ;
98
+ }
99
+
85
100
// Create the object outside the zone so its events don't trigger change detection.
86
101
// We'll bring it back in inside the `MapEventManager` only for the events that the
87
102
// 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 ) ;
103
+ if ( bounds ) {
104
+ this . _ngZone . runOutsideAngular ( ( ) => {
105
+ this . groundOverlay = new google . maps . GroundOverlay ( this . _url . getValue ( ) , bounds , {
106
+ clickable : this . clickable ,
107
+ opacity : this . _opacity . value ,
108
+ } ) ;
109
+ } ) ;
110
+ this . _assertInitialized ( ) ;
111
+ this . groundOverlay . setMap ( this . _map . googleMap ! ) ;
112
+ this . _eventManager . setTarget ( this . groundOverlay ) ;
113
+ }
95
114
} ) ;
96
115
97
116
this . _watchForOpacityChanges ( ) ;
@@ -138,19 +157,9 @@ export class MapGroundOverlay implements OnInit, OnDestroy {
138
157
return this . groundOverlay . getUrl ( ) ;
139
158
}
140
159
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
160
private _watchForOpacityChanges ( ) {
152
161
this . _opacity . pipe ( takeUntil ( this . _destroyed ) ) . subscribe ( opacity => {
153
- if ( opacity ) {
162
+ if ( opacity != null ) {
154
163
this . _assertInitialized ( ) ;
155
164
this . groundOverlay . setOpacity ( opacity ) ;
156
165
}
0 commit comments