@@ -37,7 +37,7 @@ export class MapCircle implements OnInit, OnDestroy {
3737 *
3838 * @see developers.google.com/maps/documentation/javascript/reference/polygon#Circle
3939 */
40- circle : google . maps . Circle ; // initialized in ngOnInit
40+ circle ? : google . maps . Circle ; // initialized in ngOnInit
4141
4242 @Input ( )
4343 set options ( options : google . maps . CircleOptions ) {
@@ -159,77 +159,86 @@ export class MapCircle implements OnInit, OnDestroy {
159159 constructor ( private readonly _map : GoogleMap , private readonly _ngZone : NgZone ) { }
160160
161161 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 ) ;
170173 } ) ;
171- this . circle . setMap ( this . _map . _googleMap ) ;
172- this . _eventManager . setTarget ( this . circle ) ;
173- } ) ;
174174
175- this . _watchForOptionsChanges ( ) ;
176- this . _watchForCenterChanges ( ) ;
177- this . _watchForRadiusChanges ( ) ;
175+ this . _watchForOptionsChanges ( ) ;
176+ this . _watchForCenterChanges ( ) ;
177+ this . _watchForRadiusChanges ( ) ;
178+ }
178179 }
179180
180181 ngOnDestroy ( ) {
181182 this . _eventManager . destroy ( ) ;
182183 this . _destroyed . next ( ) ;
183184 this . _destroyed . complete ( ) ;
184- this . circle . setMap ( null ) ;
185+ if ( this . circle ) {
186+ this . circle . setMap ( null ) ;
187+ }
185188 }
186189
187190 /**
188191 * @see
189192 * developers.google.com/maps/documentation/javascript/reference/polygon#Circle.getBounds
190193 */
191194 getBounds ( ) : google . maps . LatLngBounds {
192- return this . circle . getBounds ( ) ;
195+ this . _assertInitialized ( ) ;
196+ return this . circle ! . getBounds ( ) ;
193197 }
194198
195199 /**
196200 * @see
197201 * developers.google.com/maps/documentation/javascript/reference/polygon#Circle.getCenter
198202 */
199203 getCenter ( ) : google . maps . LatLng {
200- return this . circle . getCenter ( ) ;
204+ this . _assertInitialized ( ) ;
205+ return this . circle ! . getCenter ( ) ;
201206 }
202207
203208 /**
204209 * @see
205210 * developers.google.com/maps/documentation/javascript/reference/polygon#Circle.getDraggable
206211 */
207212 getDraggable ( ) : boolean {
208- return this . circle . getDraggable ( ) ;
213+ this . _assertInitialized ( ) ;
214+ return this . circle ! . getDraggable ( ) ;
209215 }
210216
211217 /**
212218 * @see
213219 * developers.google.com/maps/documentation/javascript/reference/polygon#Circle.getEditable
214220 */
215221 getEditable ( ) : boolean {
216- return this . circle . getEditable ( ) ;
222+ this . _assertInitialized ( ) ;
223+ return this . circle ! . getEditable ( ) ;
217224 }
218225
219226 /**
220227 * @see
221228 * developers.google.com/maps/documentation/javascript/reference/polygon#Circle.getCenter
222229 */
223230 getRadius ( ) : number {
224- return this . circle . getRadius ( ) ;
231+ this . _assertInitialized ( ) ;
232+ return this . circle ! . getRadius ( ) ;
225233 }
226234
227235 /**
228236 * @see
229237 * developers.google.com/maps/documentation/javascript/reference/polygon#Circle.getVisible
230238 */
231239 getVisible ( ) : boolean {
232- return this . circle . getVisible ( ) ;
240+ this . _assertInitialized ( ) ;
241+ return this . circle ! . getVisible ( ) ;
233242 }
234243
235244 private _combineOptions ( ) : Observable < google . maps . CircleOptions > {
@@ -246,23 +255,39 @@ export class MapCircle implements OnInit, OnDestroy {
246255
247256 private _watchForOptionsChanges ( ) {
248257 this . _options . pipe ( takeUntil ( this . _destroyed ) ) . subscribe ( options => {
249- this . circle . setOptions ( options ) ;
258+ this . _assertInitialized ( ) ;
259+ this . circle ! . setOptions ( options ) ;
250260 } ) ;
251261 }
252262
253263 private _watchForCenterChanges ( ) {
254264 this . _center . pipe ( takeUntil ( this . _destroyed ) ) . subscribe ( center => {
255265 if ( center ) {
256- this . circle . setCenter ( center ) ;
266+ this . _assertInitialized ( ) ;
267+ this . circle ! . setCenter ( center ) ;
257268 }
258269 } ) ;
259270 }
260271
261272 private _watchForRadiusChanges ( ) {
262273 this . _radius . pipe ( takeUntil ( this . _destroyed ) ) . subscribe ( radius => {
263274 if ( radius !== undefined ) {
264- this . circle . setRadius ( radius ) ;
275+ this . _assertInitialized ( ) ;
276+ this . circle ! . setRadius ( radius ) ;
265277 }
266278 } ) ;
267279 }
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+ }
268293}
0 commit comments