Skip to content

Commit a7f7e9b

Browse files
crisbetojelbourn
authored andcommitted
fix(google-maps): sub-components throwing during server-side rendering (#18059)
Fixes the different map-related sub-components (marker, info window and polyline) throwing errors during server-side rendering. (cherry picked from commit 149669e)
1 parent b2da537 commit a7f7e9b

File tree

6 files changed

+52
-35
lines changed

6 files changed

+52
-35
lines changed

src/google-maps/google-map/google-map.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ export const DEFAULT_WIDTH = '500px';
6565
})
6666
export class GoogleMap implements OnChanges, OnInit, OnDestroy {
6767
private _eventManager = new MapEventManager();
68-
69-
/** Whether we're currently rendering inside a browser. */
70-
private _isBrowser: boolean;
7168
private _googleMapChanges: Observable<google.maps.Map>;
7269

7370
private readonly _options = new BehaviorSubject<google.maps.MapOptions>(DEFAULT_OPTIONS);
@@ -78,6 +75,9 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
7875
private _mapEl: HTMLElement;
7976
_googleMap: UpdatedGoogleMap;
8077

78+
/** Whether we're currently rendering inside a browser. */
79+
_isBrowser: boolean;
80+
8181
@Input() height = DEFAULT_HEIGHT;
8282

8383
@Input() width = DEFAULT_WIDTH;

src/google-maps/map-info-window/map-info-window.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,16 @@ export class MapInfoWindow implements OnInit, OnDestroy {
9090
private _elementRef: ElementRef<HTMLElement>) {}
9191

9292
ngOnInit() {
93-
this._combineOptions().pipe(takeUntil(this._destroy)).subscribe(options => {
94-
if (this._infoWindow) {
95-
this._infoWindow.setOptions(options);
96-
} else {
97-
this._infoWindow = new google.maps.InfoWindow(options);
98-
this._eventManager.setTarget(this._infoWindow);
99-
}
100-
});
93+
if (this._googleMap._isBrowser) {
94+
this._combineOptions().pipe(takeUntil(this._destroy)).subscribe(options => {
95+
if (this._infoWindow) {
96+
this._infoWindow.setOptions(options);
97+
} else {
98+
this._infoWindow = new google.maps.InfoWindow(options);
99+
this._eventManager.setTarget(this._infoWindow);
100+
}
101+
});
102+
}
101103
}
102104

103105
ngOnDestroy() {
@@ -147,7 +149,7 @@ export class MapInfoWindow implements OnInit, OnDestroy {
147149
*/
148150
open(anchor?: MapMarker) {
149151
const marker = anchor ? anchor._marker : undefined;
150-
if (this._googleMap._googleMap) {
152+
if (this._googleMap._googleMap && this._infoWindow) {
151153
this._elementRef.nativeElement.style.display = '';
152154
this._infoWindow!.open(this._googleMap._googleMap, marker);
153155
}

src/google-maps/map-marker/map-marker.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -239,19 +239,21 @@ export class MapMarker implements OnInit, OnDestroy {
239239
constructor(private readonly _googleMap: GoogleMap) {}
240240

241241
ngOnInit() {
242-
const combinedOptionsChanges = this._combineOptions();
243-
244-
combinedOptionsChanges.pipe(take(1)).subscribe(options => {
245-
this._marker = new google.maps.Marker(options);
246-
this._marker.setMap(this._googleMap._googleMap);
247-
this._eventManager.setTarget(this._marker);
248-
});
249-
250-
this._watchForOptionsChanges();
251-
this._watchForTitleChanges();
252-
this._watchForPositionChanges();
253-
this._watchForLabelChanges();
254-
this._watchForClickableChanges();
242+
if (this._googleMap._isBrowser) {
243+
const combinedOptionsChanges = this._combineOptions();
244+
245+
combinedOptionsChanges.pipe(take(1)).subscribe(options => {
246+
this._marker = new google.maps.Marker(options);
247+
this._marker.setMap(this._googleMap._googleMap);
248+
this._eventManager.setTarget(this._marker);
249+
});
250+
251+
this._watchForOptionsChanges();
252+
this._watchForTitleChanges();
253+
this._watchForPositionChanges();
254+
this._watchForLabelChanges();
255+
this._watchForClickableChanges();
256+
}
255257
}
256258

257259
ngOnDestroy() {

src/google-maps/map-polyline/map-polyline.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,18 @@ export class MapPolyline implements OnInit, OnDestroy {
132132
constructor(private readonly _map: GoogleMap) {}
133133

134134
ngOnInit() {
135-
const combinedOptionsChanges = this._combineOptions();
135+
if (this._map._isBrowser) {
136+
const combinedOptionsChanges = this._combineOptions();
136137

137-
combinedOptionsChanges.pipe(take(1)).subscribe(options => {
138-
this._polyline = new google.maps.Polyline(options);
139-
this._polyline.setMap(this._map._googleMap);
140-
this._eventManager.setTarget(this._polyline);
141-
});
138+
combinedOptionsChanges.pipe(take(1)).subscribe(options => {
139+
this._polyline = new google.maps.Polyline(options);
140+
this._polyline.setMap(this._map._googleMap);
141+
this._eventManager.setTarget(this._polyline);
142+
});
142143

143-
this._watchForOptionsChanges();
144-
this._watchForPathChanges();
144+
this._watchForOptionsChanges();
145+
this._watchForPathChanges();
146+
}
145147
}
146148

147149
ngOnDestroy() {
@@ -151,7 +153,9 @@ export class MapPolyline implements OnInit, OnDestroy {
151153
for (let listener of this._listeners) {
152154
listener.remove();
153155
}
154-
this._polyline.setMap(null);
156+
if (this._polyline) {
157+
this._polyline.setMap(null);
158+
}
155159
}
156160

157161
/**

src/universal-app/kitchen-sink/kitchen-sink.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,4 +384,12 @@ <h2>YouTube player</h2>
384384
<youtube-player videoId="dQw4w9WgXcQ"></youtube-player>
385385

386386
<h2>Google Map</h2>
387-
<google-map height="400px" width="750px"></google-map>
387+
<google-map height="400px" width="750px">
388+
<map-marker [position]="{lat: 24, lng: 12}"></map-marker>
389+
<map-info-window>Hello</map-info-window>
390+
<map-polyline [options]="{
391+
path: [{lat: 25, lng: 26}, {lat: 26, lng: 27}, {lat: 30, lng: 34}],
392+
strokeColor: 'grey',
393+
strokeOpacity: 0.8
394+
}"></map-polyline>
395+
</google-map>

tools/public_api_guard/google-maps/google-maps.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export declare class GoogleMap implements OnChanges, OnInit, OnDestroy {
22
_googleMap: UpdatedGoogleMap;
3+
_isBrowser: boolean;
34
boundsChanged: Observable<void>;
45
center: google.maps.LatLngLiteral | google.maps.LatLng;
56
centerChanged: Observable<void>;

0 commit comments

Comments
 (0)