Skip to content

Commit b2d08df

Browse files
authored
fix(google-maps): server-side rendering error for polygon and rectangle components (#18573)
Fixes a server-side rendering error being thrown if a polygon or rectangle is used.
1 parent ea03cce commit b2d08df

File tree

5 files changed

+45
-35
lines changed

5 files changed

+45
-35
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,7 @@ export class MapMarker implements OnInit, OnDestroy {
243243

244244
ngOnInit() {
245245
if (this._googleMap._isBrowser) {
246-
const combinedOptionsChanges = this._combineOptions();
247-
248-
combinedOptionsChanges.pipe(take(1)).subscribe(options => {
246+
this._combineOptions().pipe(take(1)).subscribe(options => {
249247
// Create the object outside the zone so its events don't trigger change detection.
250248
// We'll bring it back in inside the `MapEventManager` only for the events that the
251249
// user has subscribed to.

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,28 +134,30 @@ export class MapPolygon implements OnInit, OnDestroy {
134134
constructor(private readonly _map: GoogleMap, private readonly _ngZone: NgZone) {}
135135

136136
ngOnInit() {
137-
const combinedOptionsChanges = this._combineOptions();
138-
139-
combinedOptionsChanges.pipe(take(1)).subscribe(options => {
140-
// Create the object outside the zone so its events don't trigger change detection.
141-
// We'll bring it back in inside the `MapEventManager` only for the events that the
142-
// user has subscribed to.
143-
this._ngZone.runOutsideAngular(() => {
144-
this._polygon = new google.maps.Polygon(options);
137+
if (this._map._isBrowser) {
138+
this._combineOptions().pipe(take(1)).subscribe(options => {
139+
// Create the object outside the zone so its events don't trigger change detection.
140+
// We'll bring it back in inside the `MapEventManager` only for the events that the
141+
// user has subscribed to.
142+
this._ngZone.runOutsideAngular(() => {
143+
this._polygon = new google.maps.Polygon(options);
144+
});
145+
this._polygon.setMap(this._map._googleMap);
146+
this._eventManager.setTarget(this._polygon);
145147
});
146-
this._polygon.setMap(this._map._googleMap);
147-
this._eventManager.setTarget(this._polygon);
148-
});
149148

150-
this._watchForOptionsChanges();
151-
this._watchForPathChanges();
149+
this._watchForOptionsChanges();
150+
this._watchForPathChanges();
151+
}
152152
}
153153

154154
ngOnDestroy() {
155155
this._eventManager.destroy();
156156
this._destroyed.next();
157157
this._destroyed.complete();
158-
this._polygon.setMap(null);
158+
if (this._polygon) {
159+
this._polygon.setMap(null);
160+
}
159161
}
160162

161163
/**

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,7 @@ export class MapPolyline implements OnInit, OnDestroy {
135135

136136
ngOnInit() {
137137
if (this._map._isBrowser) {
138-
const combinedOptionsChanges = this._combineOptions();
139-
140-
combinedOptionsChanges.pipe(take(1)).subscribe(options => {
138+
this._combineOptions().pipe(take(1)).subscribe(options => {
141139
// Create the object outside the zone so its events don't trigger change detection.
142140
// We'll bring it back in inside the `MapEventManager` only for the events that the
143141
// user has subscribed to.

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -142,28 +142,30 @@ export class MapRectangle implements OnInit, OnDestroy {
142142
constructor(private readonly _map: GoogleMap, private readonly _ngZone: NgZone) {}
143143

144144
ngOnInit() {
145-
const combinedOptionsChanges = this._combineOptions();
146-
147-
combinedOptionsChanges.pipe(take(1)).subscribe(options => {
148-
// Create the object outside the zone so its events don't trigger change detection.
149-
// We'll bring it back in inside the `MapEventManager` only for the events that the
150-
// user has subscribed to.
151-
this._ngZone.runOutsideAngular(() => {
152-
this._rectangle = new google.maps.Rectangle(options);
145+
if (this._map._isBrowser) {
146+
this._combineOptions().pipe(take(1)).subscribe(options => {
147+
// Create the object outside the zone so its events don't trigger change detection.
148+
// We'll bring it back in inside the `MapEventManager` only for the events that the
149+
// user has subscribed to.
150+
this._ngZone.runOutsideAngular(() => {
151+
this._rectangle = new google.maps.Rectangle(options);
152+
});
153+
this._rectangle.setMap(this._map._googleMap);
154+
this._eventManager.setTarget(this._rectangle);
153155
});
154-
this._rectangle.setMap(this._map._googleMap);
155-
this._eventManager.setTarget(this._rectangle);
156-
});
157156

158-
this._watchForOptionsChanges();
159-
this._watchForBoundsChanges();
157+
this._watchForOptionsChanges();
158+
this._watchForBoundsChanges();
159+
}
160160
}
161161

162162
ngOnDestroy() {
163163
this._eventManager.destroy();
164164
this._destroyed.next();
165165
this._destroyed.complete();
166-
this._rectangle.setMap(null);
166+
if (this._rectangle) {
167+
this._rectangle.setMap(null);
168+
}
167169
}
168170

169171
/**

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ <h2>Drag and Drop</h2>
375375
<h2>Virtual scroll</h2>
376376

377377
<cdk-virtual-scroll-viewport itemSize="50" class="universal-viewport">
378-
<div *cdkVirtualFor="let size of virtualScrollData; let i = index" style="height: 50px">
378+
<div *cdkVirtualFor="let size of virtualScrollData; let i = index" style="height: 50px;">
379379
Item #{{i}}
380380
</div>
381381
</cdk-virtual-scroll-viewport>
@@ -392,4 +392,14 @@ <h2>Google Map</h2>
392392
strokeColor: 'grey',
393393
strokeOpacity: 0.8
394394
}"></map-polyline>
395+
<map-polygon [options]="{
396+
paths: [{lat: 20, lng: 21}, {lat: 22, lng: 23}, {lat: 24, lng: 25}],
397+
strokeColor: 'grey',
398+
strokeOpacity: 0.8
399+
}"></map-polygon>
400+
<map-rectangle [options]="{
401+
bounds: {east: 30, north: 15, west: 10, south: -5},
402+
strokeColor: 'grey',
403+
strokeOpacity: 0.8
404+
}"></map-rectangle>
395405
</google-map>

0 commit comments

Comments
 (0)