@@ -37,7 +37,7 @@ export class MapCircle implements OnInit, OnDestroy {
37
37
*
38
38
* @see developers.google.com/maps/documentation/javascript/reference/polygon#Circle
39
39
*/
40
- circle : google . maps . Circle ; // initialized in ngOnInit
40
+ circle ? : google . maps . Circle ; // initialized in ngOnInit
41
41
42
42
@Input ( )
43
43
set options ( options : google . maps . CircleOptions ) {
@@ -159,77 +159,86 @@ export class MapCircle implements OnInit, OnDestroy {
159
159
constructor ( private readonly _map : GoogleMap , private readonly _ngZone : NgZone ) { }
160
160
161
161
ngOnInit ( ) {
162
- const combinedOptionsChanges = this . _combineOptions ( ) ;
163
-
164
- combinedOptionsChanges . pipe ( take ( 1 ) ) . subscribe ( options => {
165
- // Create the object outside the zone so its events don't trigger change detection.
166
- // We'll bring it back in inside the `MapEventManager` only for the events that the
167
- // user has subscribed to.
168
- this . _ngZone . runOutsideAngular ( ( ) => {
169
- this . circle = new google . maps . Circle ( options ) ;
162
+ if ( this . _map . _isBrowser ) {
163
+ this . _combineOptions ( ) . pipe ( take ( 1 ) ) . subscribe ( options => {
164
+ // Create the object outside the zone so its events don't trigger change detection.
165
+ // We'll bring it back in inside the `MapEventManager` only for the events that the
166
+ // user has subscribed to.
167
+ this . _ngZone . runOutsideAngular ( ( ) => {
168
+ this . circle = new google . maps . Circle ( options ) ;
169
+ } ) ;
170
+ this . _assertInitialized ( ) ;
171
+ this . circle ! . setMap ( this . _map . googleMap ! ) ;
172
+ this . _eventManager . setTarget ( this . circle ) ;
170
173
} ) ;
171
- this . circle . setMap ( this . _map . _googleMap ) ;
172
- this . _eventManager . setTarget ( this . circle ) ;
173
- } ) ;
174
174
175
- this . _watchForOptionsChanges ( ) ;
176
- this . _watchForCenterChanges ( ) ;
177
- this . _watchForRadiusChanges ( ) ;
175
+ this . _watchForOptionsChanges ( ) ;
176
+ this . _watchForCenterChanges ( ) ;
177
+ this . _watchForRadiusChanges ( ) ;
178
+ }
178
179
}
179
180
180
181
ngOnDestroy ( ) {
181
182
this . _eventManager . destroy ( ) ;
182
183
this . _destroyed . next ( ) ;
183
184
this . _destroyed . complete ( ) ;
184
- this . circle . setMap ( null ) ;
185
+ if ( this . circle ) {
186
+ this . circle . setMap ( null ) ;
187
+ }
185
188
}
186
189
187
190
/**
188
191
* @see
189
192
* developers.google.com/maps/documentation/javascript/reference/polygon#Circle.getBounds
190
193
*/
191
194
getBounds ( ) : google . maps . LatLngBounds {
192
- return this . circle . getBounds ( ) ;
195
+ this . _assertInitialized ( ) ;
196
+ return this . circle ! . getBounds ( ) ;
193
197
}
194
198
195
199
/**
196
200
* @see
197
201
* developers.google.com/maps/documentation/javascript/reference/polygon#Circle.getCenter
198
202
*/
199
203
getCenter ( ) : google . maps . LatLng {
200
- return this . circle . getCenter ( ) ;
204
+ this . _assertInitialized ( ) ;
205
+ return this . circle ! . getCenter ( ) ;
201
206
}
202
207
203
208
/**
204
209
* @see
205
210
* developers.google.com/maps/documentation/javascript/reference/polygon#Circle.getDraggable
206
211
*/
207
212
getDraggable ( ) : boolean {
208
- return this . circle . getDraggable ( ) ;
213
+ this . _assertInitialized ( ) ;
214
+ return this . circle ! . getDraggable ( ) ;
209
215
}
210
216
211
217
/**
212
218
* @see
213
219
* developers.google.com/maps/documentation/javascript/reference/polygon#Circle.getEditable
214
220
*/
215
221
getEditable ( ) : boolean {
216
- return this . circle . getEditable ( ) ;
222
+ this . _assertInitialized ( ) ;
223
+ return this . circle ! . getEditable ( ) ;
217
224
}
218
225
219
226
/**
220
227
* @see
221
228
* developers.google.com/maps/documentation/javascript/reference/polygon#Circle.getCenter
222
229
*/
223
230
getRadius ( ) : number {
224
- return this . circle . getRadius ( ) ;
231
+ this . _assertInitialized ( ) ;
232
+ return this . circle ! . getRadius ( ) ;
225
233
}
226
234
227
235
/**
228
236
* @see
229
237
* developers.google.com/maps/documentation/javascript/reference/polygon#Circle.getVisible
230
238
*/
231
239
getVisible ( ) : boolean {
232
- return this . circle . getVisible ( ) ;
240
+ this . _assertInitialized ( ) ;
241
+ return this . circle ! . getVisible ( ) ;
233
242
}
234
243
235
244
private _combineOptions ( ) : Observable < google . maps . CircleOptions > {
@@ -246,23 +255,39 @@ export class MapCircle implements OnInit, OnDestroy {
246
255
247
256
private _watchForOptionsChanges ( ) {
248
257
this . _options . pipe ( takeUntil ( this . _destroyed ) ) . subscribe ( options => {
249
- this . circle . setOptions ( options ) ;
258
+ this . _assertInitialized ( ) ;
259
+ this . circle ! . setOptions ( options ) ;
250
260
} ) ;
251
261
}
252
262
253
263
private _watchForCenterChanges ( ) {
254
264
this . _center . pipe ( takeUntil ( this . _destroyed ) ) . subscribe ( center => {
255
265
if ( center ) {
256
- this . circle . setCenter ( center ) ;
266
+ this . _assertInitialized ( ) ;
267
+ this . circle ! . setCenter ( center ) ;
257
268
}
258
269
} ) ;
259
270
}
260
271
261
272
private _watchForRadiusChanges ( ) {
262
273
this . _radius . pipe ( takeUntil ( this . _destroyed ) ) . subscribe ( radius => {
263
274
if ( radius !== undefined ) {
264
- this . circle . setRadius ( radius ) ;
275
+ this . _assertInitialized ( ) ;
276
+ this . circle ! . setRadius ( radius ) ;
265
277
}
266
278
} ) ;
267
279
}
280
+
281
+ private _assertInitialized ( ) {
282
+ if ( ! this . _map . googleMap ) {
283
+ throw Error (
284
+ 'Cannot access Google Map information before the API has been initialized. ' +
285
+ 'Please wait for the API to load before trying to interact with it.' ) ;
286
+ }
287
+ if ( ! this . circle ) {
288
+ throw Error (
289
+ 'Cannot interact with a Google Map Circle before it has been ' +
290
+ 'initialized. Please wait for the Circle to load before trying to interact with it.' ) ;
291
+ }
292
+ }
268
293
}
0 commit comments