Skip to content

Commit 591ac9c

Browse files
authored
fix(google-maps): avoid errors when accessing API too early (#18376)
Fixes a bunch of errors that are thrown if we access the Google Maps-dependent methods before the API has loaded. We had everything typed correctly, but in all cases we were overriding the null checks.
1 parent 633e4c2 commit 591ac9c

File tree

4 files changed

+27
-24
lines changed

4 files changed

+27
-24
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export class MapInfoWindow implements OnInit, OnDestroy {
131131
* developers.google.com/maps/documentation/javascript/reference/info-window#InfoWindow.getContent
132132
*/
133133
getContent(): string|Node {
134-
return this._infoWindow!.getContent();
134+
return this._infoWindow ? this._infoWindow.getContent() : '';
135135
}
136136

137137
/**
@@ -140,15 +140,15 @@ export class MapInfoWindow implements OnInit, OnDestroy {
140140
* #InfoWindow.getPosition
141141
*/
142142
getPosition(): google.maps.LatLng|null {
143-
return this._infoWindow!.getPosition() || null;
143+
return this._infoWindow ? this._infoWindow.getPosition() : null;
144144
}
145145

146146
/**
147147
* See
148148
* developers.google.com/maps/documentation/javascript/reference/info-window#InfoWindow.getZIndex
149149
*/
150150
getZIndex(): number {
151-
return this._infoWindow!.getZIndex();
151+
return this._infoWindow ? this._infoWindow.getZIndex() : -1;
152152
}
153153

154154
/**

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -276,95 +276,95 @@ export class MapMarker implements OnInit, OnDestroy {
276276
* developers.google.com/maps/documentation/javascript/reference/marker#Marker.getAnimation
277277
*/
278278
getAnimation(): google.maps.Animation|null {
279-
return this._marker!.getAnimation() || null;
279+
return (this._marker && this._marker.getAnimation()) || null;
280280
}
281281

282282
/**
283283
* See
284284
* developers.google.com/maps/documentation/javascript/reference/marker#Marker.getClickable
285285
*/
286286
getClickable(): boolean {
287-
return this._marker!.getClickable();
287+
return this._marker ? this._marker.getClickable() : false;
288288
}
289289

290290
/**
291291
* See
292292
* developers.google.com/maps/documentation/javascript/reference/marker#Marker.getCursor
293293
*/
294294
getCursor(): string|null {
295-
return this._marker!.getCursor() || null;
295+
return (this._marker && this._marker.getCursor()) || null;
296296
}
297297

298298
/**
299299
* See
300300
* developers.google.com/maps/documentation/javascript/reference/marker#Marker.getDraggable
301301
*/
302302
getDraggable(): boolean {
303-
return !!this._marker!.getDraggable();
303+
return this._marker ? !!this._marker.getDraggable() : false;
304304
}
305305

306306
/**
307307
* See
308308
* developers.google.com/maps/documentation/javascript/reference/marker#Marker.getIcon
309309
*/
310310
getIcon(): string|google.maps.Icon|google.maps.Symbol|null {
311-
return this._marker!.getIcon() || null;
311+
return (this._marker && this._marker.getIcon()) || null;
312312
}
313313

314314
/**
315315
* See
316316
* developers.google.com/maps/documentation/javascript/reference/marker#Marker.getLabel
317317
*/
318318
getLabel(): google.maps.MarkerLabel|null {
319-
return this._marker!.getLabel() || null;
319+
return (this._marker && this._marker.getLabel()) || null;
320320
}
321321

322322
/**
323323
* See
324324
* developers.google.com/maps/documentation/javascript/reference/marker#Marker.getOpacity
325325
*/
326326
getOpacity(): number|null {
327-
return this._marker!.getOpacity() || null;
327+
return (this._marker && this._marker.getOpacity()) || null;
328328
}
329329

330330
/**
331331
* See
332332
* developers.google.com/maps/documentation/javascript/reference/marker#Marker.getPosition
333333
*/
334334
getPosition(): google.maps.LatLng|null {
335-
return this._marker!.getPosition() || null;
335+
return (this._marker && this._marker.getPosition()) || null;
336336
}
337337

338338
/**
339339
* See
340340
* developers.google.com/maps/documentation/javascript/reference/marker#Marker.getShape
341341
*/
342342
getShape(): google.maps.MarkerShape|null {
343-
return this._marker!.getShape() || null;
343+
return (this._marker && this._marker.getShape()) || null;
344344
}
345345

346346
/**
347347
* See
348348
* developers.google.com/maps/documentation/javascript/reference/marker#Marker.getTitle
349349
*/
350350
getTitle(): string|null {
351-
return this._marker!.getTitle() || null;
351+
return (this._marker && this._marker.getTitle()) || null;
352352
}
353353

354354
/**
355355
* See
356356
* developers.google.com/maps/documentation/javascript/reference/marker#Marker.getVisible
357357
*/
358358
getVisible(): boolean {
359-
return this._marker!.getVisible();
359+
return this._marker ? this._marker.getVisible() : false;
360360
}
361361

362362
/**
363363
* See
364364
* developers.google.com/maps/documentation/javascript/reference/marker#Marker.getZIndex
365365
*/
366366
getZIndex(): number|null {
367-
return this._marker!.getZIndex() || null;
367+
return (this._marker && this._marker.getZIndex()) || null;
368368
}
369369

370370
private _combineOptions(): Observable<google.maps.MarkerOptions> {

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class MapPolyline implements OnInit, OnDestroy {
3939

4040
private readonly _destroyed = new Subject<void>();
4141

42-
_polyline: google.maps.Polyline; // initialized in ngOnInit
42+
_polyline?: google.maps.Polyline; // initialized in ngOnInit
4343

4444
@Input()
4545
set options(options: google.maps.PolylineOptions) {
@@ -142,7 +142,7 @@ export class MapPolyline implements OnInit, OnDestroy {
142142
// We'll bring it back in inside the `MapEventManager` only for the events that the
143143
// user has subscribed to.
144144
this._ngZone.runOutsideAngular(() => this._polyline = new google.maps.Polyline(options));
145-
this._polyline.setMap(this._map._googleMap);
145+
this._polyline!.setMap(this._map._googleMap);
146146
this._eventManager.setTarget(this._polyline);
147147
});
148148

@@ -165,28 +165,29 @@ export class MapPolyline implements OnInit, OnDestroy {
165165
* developers.google.com/maps/documentation/javascript/reference/polygon#Polyline.getDraggable
166166
*/
167167
getDraggable(): boolean {
168-
return this._polyline.getDraggable();
168+
return this._polyline ? this._polyline.getDraggable() : false;
169169
}
170170

171171
/**
172172
* @see developers.google.com/maps/documentation/javascript/reference/polygon#Polyline.getEditable
173173
*/
174174
getEditable(): boolean {
175-
return this._polyline.getEditable();
175+
return this._polyline ? this._polyline.getEditable() : false;
176176
}
177177

178178
/**
179179
* @see developers.google.com/maps/documentation/javascript/reference/polygon#Polyline.getPath
180180
*/
181181
getPath(): google.maps.MVCArray<google.maps.LatLng> {
182-
return this._polyline.getPath();
182+
// @breaking-change 11.0.0 Make the return value nullable.
183+
return this._polyline ? this._polyline.getPath() : null!;
183184
}
184185

185186
/**
186187
* @see developers.google.com/maps/documentation/javascript/reference/polygon#Polyline.getVisible
187188
*/
188189
getVisible(): boolean {
189-
return this._polyline.getVisible();
190+
return this._polyline ? this._polyline.getVisible() : false;
190191
}
191192

192193
private _combineOptions(): Observable<google.maps.PolylineOptions> {
@@ -201,13 +202,15 @@ export class MapPolyline implements OnInit, OnDestroy {
201202

202203
private _watchForOptionsChanges() {
203204
this._options.pipe(takeUntil(this._destroyed)).subscribe(options => {
204-
this._polyline.setOptions(options);
205+
if (this._polyline) {
206+
this._polyline.setOptions(options);
207+
}
205208
});
206209
}
207210

208211
private _watchForPathChanges() {
209212
this._path.pipe(takeUntil(this._destroyed)).subscribe(path => {
210-
if (path) {
213+
if (path && this._polyline) {
211214
this._polyline.setPath(path);
212215
}
213216
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export declare class MapPolygon implements OnInit, OnDestroy {
150150
}
151151

152152
export declare class MapPolyline implements OnInit, OnDestroy {
153-
_polyline: google.maps.Polyline;
153+
_polyline?: google.maps.Polyline;
154154
set options(options: google.maps.PolylineOptions);
155155
set path(path: google.maps.MVCArray<google.maps.LatLng> | google.maps.LatLng[] | google.maps.LatLngLiteral[]);
156156
polylineClick: Observable<google.maps.PolyMouseEvent>;

0 commit comments

Comments
 (0)