Skip to content

fix(google-maps): server-side rendering error for polygon and rectangle components #18573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/google-maps/map-marker/map-marker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,7 @@ export class MapMarker implements OnInit, OnDestroy {

ngOnInit() {
if (this._googleMap._isBrowser) {
const combinedOptionsChanges = this._combineOptions();

combinedOptionsChanges.pipe(take(1)).subscribe(options => {
this._combineOptions().pipe(take(1)).subscribe(options => {
// Create the object outside the zone so its events don't trigger change detection.
// We'll bring it back in inside the `MapEventManager` only for the events that the
// user has subscribed to.
Expand Down
30 changes: 16 additions & 14 deletions src/google-maps/map-polygon/map-polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,28 +134,30 @@ export class MapPolygon implements OnInit, OnDestroy {
constructor(private readonly _map: GoogleMap, private readonly _ngZone: NgZone) {}

ngOnInit() {
const combinedOptionsChanges = this._combineOptions();

combinedOptionsChanges.pipe(take(1)).subscribe(options => {
// Create the object outside the zone so its events don't trigger change detection.
// We'll bring it back in inside the `MapEventManager` only for the events that the
// user has subscribed to.
this._ngZone.runOutsideAngular(() => {
this._polygon = new google.maps.Polygon(options);
if (this._map._isBrowser) {
this._combineOptions().pipe(take(1)).subscribe(options => {
// Create the object outside the zone so its events don't trigger change detection.
// We'll bring it back in inside the `MapEventManager` only for the events that the
// user has subscribed to.
this._ngZone.runOutsideAngular(() => {
this._polygon = new google.maps.Polygon(options);
});
this._polygon.setMap(this._map._googleMap);
this._eventManager.setTarget(this._polygon);
});
this._polygon.setMap(this._map._googleMap);
this._eventManager.setTarget(this._polygon);
});

this._watchForOptionsChanges();
this._watchForPathChanges();
this._watchForOptionsChanges();
this._watchForPathChanges();
}
}

ngOnDestroy() {
this._eventManager.destroy();
this._destroyed.next();
this._destroyed.complete();
this._polygon.setMap(null);
if (this._polygon) {
this._polygon.setMap(null);
}
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/google-maps/map-polyline/map-polyline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,7 @@ export class MapPolyline implements OnInit, OnDestroy {

ngOnInit() {
if (this._map._isBrowser) {
const combinedOptionsChanges = this._combineOptions();

combinedOptionsChanges.pipe(take(1)).subscribe(options => {
this._combineOptions().pipe(take(1)).subscribe(options => {
// Create the object outside the zone so its events don't trigger change detection.
// We'll bring it back in inside the `MapEventManager` only for the events that the
// user has subscribed to.
Expand Down
30 changes: 16 additions & 14 deletions src/google-maps/map-rectangle/map-rectangle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,28 +142,30 @@ export class MapRectangle implements OnInit, OnDestroy {
constructor(private readonly _map: GoogleMap, private readonly _ngZone: NgZone) {}

ngOnInit() {
const combinedOptionsChanges = this._combineOptions();

combinedOptionsChanges.pipe(take(1)).subscribe(options => {
// Create the object outside the zone so its events don't trigger change detection.
// We'll bring it back in inside the `MapEventManager` only for the events that the
// user has subscribed to.
this._ngZone.runOutsideAngular(() => {
this._rectangle = new google.maps.Rectangle(options);
if (this._map._isBrowser) {
this._combineOptions().pipe(take(1)).subscribe(options => {
// Create the object outside the zone so its events don't trigger change detection.
// We'll bring it back in inside the `MapEventManager` only for the events that the
// user has subscribed to.
this._ngZone.runOutsideAngular(() => {
this._rectangle = new google.maps.Rectangle(options);
});
this._rectangle.setMap(this._map._googleMap);
this._eventManager.setTarget(this._rectangle);
});
this._rectangle.setMap(this._map._googleMap);
this._eventManager.setTarget(this._rectangle);
});

this._watchForOptionsChanges();
this._watchForBoundsChanges();
this._watchForOptionsChanges();
this._watchForBoundsChanges();
}
}

ngOnDestroy() {
this._eventManager.destroy();
this._destroyed.next();
this._destroyed.complete();
this._rectangle.setMap(null);
if (this._rectangle) {
this._rectangle.setMap(null);
}
}

/**
Expand Down
12 changes: 11 additions & 1 deletion src/universal-app/kitchen-sink/kitchen-sink.html
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ <h2>Drag and Drop</h2>
<h2>Virtual scroll</h2>

<cdk-virtual-scroll-viewport itemSize="50" class="universal-viewport">
<div *cdkVirtualFor="let size of virtualScrollData; let i = index" style="height: 50px">
<div *cdkVirtualFor="let size of virtualScrollData; let i = index" style="height: 50px;">
Item #{{i}}
</div>
</cdk-virtual-scroll-viewport>
Expand All @@ -392,4 +392,14 @@ <h2>Google Map</h2>
strokeColor: 'grey',
strokeOpacity: 0.8
}"></map-polyline>
<map-polygon [options]="{
paths: [{lat: 20, lng: 21}, {lat: 22, lng: 23}, {lat: 24, lng: 25}],
strokeColor: 'grey',
strokeOpacity: 0.8
}"></map-polygon>
<map-rectangle [options]="{
bounds: {east: 30, north: 15, west: 10, south: -5},
strokeColor: 'grey',
strokeOpacity: 0.8
}"></map-rectangle>
</google-map>