Skip to content

Commit c71bbf2

Browse files
authored
fix(google-maps): map circle error during server-side rendering (#18822)
Fixes the `map-circle` component throwing an error during server-side rendering.
1 parent 24a7c6d commit c71bbf2

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -159,29 +159,32 @@ 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.circle.setMap(this._map._googleMap);
171+
this._eventManager.setTarget(this.circle);
170172
});
171-
this.circle.setMap(this._map._googleMap);
172-
this._eventManager.setTarget(this.circle);
173-
});
174173

175-
this._watchForOptionsChanges();
176-
this._watchForCenterChanges();
177-
this._watchForRadiusChanges();
174+
this._watchForOptionsChanges();
175+
this._watchForCenterChanges();
176+
this._watchForRadiusChanges();
177+
}
178178
}
179179

180180
ngOnDestroy() {
181181
this._eventManager.destroy();
182182
this._destroyed.next();
183183
this._destroyed.complete();
184-
this.circle.setMap(null);
184+
185+
if (this.circle) {
186+
this.circle.setMap(null);
187+
}
185188
}
186189

187190
/**

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,4 +402,10 @@ <h2>Google Map</h2>
402402
strokeColor: 'grey',
403403
strokeOpacity: 0.8
404404
}"></map-rectangle>
405+
<map-circle [options]="{
406+
center: {lat: 19, lng: 20},
407+
radius: 500000,
408+
strokeColor: 'grey',
409+
strokeOpacity: 0.8
410+
}"></map-circle>
405411
</google-map>

0 commit comments

Comments
 (0)