Skip to content

Commit 12d4033

Browse files
committed
fix(google-maps): instantiate geocoder lazily
Currently we instantiate the `Geocoder` when the `MapGeocoder` provider is instantiated which may be too early if the Google Maps API is loaded lazily. These changes switch to creating it only when `geocode` is called. Fixes #22148.
1 parent 4aa48a1 commit 12d4033

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,21 @@ export interface MapGeocoderResponse {
2323
*/
2424
@Injectable({providedIn: 'root'})
2525
export class MapGeocoder {
26-
private readonly _geocoder: google.maps.Geocoder;
26+
private _geocoder: google.maps.Geocoder|undefined;
2727

28-
constructor(private readonly _ngZone: NgZone) {
29-
this._geocoder = new google.maps.Geocoder();
30-
}
28+
constructor(private readonly _ngZone: NgZone) {}
3129

3230
/**
3331
* See developers.google.com/maps/documentation/javascript/reference/geocoder#Geocoder.geocode
3432
*/
3533
geocode(request: google.maps.GeocoderRequest): Observable<MapGeocoderResponse> {
3634
return new Observable(observer => {
35+
// Initialize the `Geocoder` lazily since the Google Maps API may
36+
// not have been loaded when the provider is instantiated.
37+
if (!this._geocoder) {
38+
this._geocoder = new google.maps.Geocoder();
39+
}
40+
3741
this._geocoder.geocode(request, (results, status) => {
3842
this._ngZone.run(() => {
3943
observer.next({results, status});

0 commit comments

Comments
 (0)