@@ -48,6 +48,12 @@ export const DEFAULT_HEIGHT = '500px';
4848/** Arbitrary default width for the map element */
4949export const DEFAULT_WIDTH = '500px' ;
5050
51+ /**
52+ * Whether we're currently rendering inside a browser. Equivalent of `Platform.isBrowser`,
53+ * but copied over here so we don't have to add another dependency.
54+ */
55+ const isBrowser = typeof window === 'object' && ! ! window ;
56+
5157/**
5258 * Angular component that renders a Google Map via the Google Maps JavaScript
5359 * API.
@@ -200,13 +206,15 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
200206 private readonly _destroy = new Subject < void > ( ) ;
201207
202208 constructor ( private readonly _elementRef : ElementRef ) {
203- const googleMapsWindow : GoogleMapsWindow = window ;
204- if ( ! googleMapsWindow . google ) {
205- throw Error (
206- 'Namespace google not found, cannot construct embedded google ' +
207- 'map. Please install the Google Maps JavaScript API: ' +
208- 'https://developers.google.com/maps/documentation/javascript/' +
209- 'tutorial#Loading_the_Maps_API' ) ;
209+ if ( isBrowser ) {
210+ const googleMapsWindow : GoogleMapsWindow = window ;
211+ if ( ! googleMapsWindow . google ) {
212+ throw Error (
213+ 'Namespace google not found, cannot construct embedded google ' +
214+ 'map. Please install the Google Maps JavaScript API: ' +
215+ 'https://developers.google.com/maps/documentation/javascript/' +
216+ 'tutorial#Loading_the_Maps_API' ) ;
217+ }
210218 }
211219 }
212220
@@ -215,21 +223,24 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
215223 }
216224
217225 ngOnInit ( ) {
218- this . _mapEl = this . _elementRef . nativeElement . querySelector ( '.map-container' ) ! ;
219- this . _setSize ( ) ;
226+ // It should be a noop during server-side rendering.
227+ if ( isBrowser ) {
228+ this . _mapEl = this . _elementRef . nativeElement . querySelector ( '.map-container' ) ! ;
229+ this . _setSize ( ) ;
220230
221- const combinedOptionsChanges = this . _combineOptions ( ) ;
231+ const combinedOptionsChanges = this . _combineOptions ( ) ;
222232
223- this . _googleMapChanges = this . _initializeMap ( combinedOptionsChanges ) ;
224- this . _googleMapChanges . subscribe ( ( googleMap : google . maps . Map ) => {
225- this . _googleMap = googleMap as UpdatedGoogleMap ;
233+ this . _googleMapChanges = this . _initializeMap ( combinedOptionsChanges ) ;
234+ this . _googleMapChanges . subscribe ( ( googleMap : google . maps . Map ) => {
235+ this . _googleMap = googleMap as UpdatedGoogleMap ;
226236
227- this . _initializeEventHandlers ( ) ;
228- } ) ;
237+ this . _initializeEventHandlers ( ) ;
238+ } ) ;
229239
230- this . _watchForOptionsChanges ( ) ;
231- this . _watchForCenterChanges ( ) ;
232- this . _watchForZoomChanges ( ) ;
240+ this . _watchForOptionsChanges ( ) ;
241+ this . _watchForCenterChanges ( ) ;
242+ this . _watchForZoomChanges ( ) ;
243+ }
233244 }
234245
235246 ngOnDestroy ( ) {
0 commit comments