Skip to content

Commit 7c9bf99

Browse files
authored
refactor(google-maps): switch to inject function (#29774)
Reworks the Google Maps module to use the `inject` function instead of constructor-based injection.
1 parent 9b4085c commit 7c9bf99

File tree

18 files changed

+105
-94
lines changed

18 files changed

+105
-94
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
OnInit,
2020
Output,
2121
ViewEncapsulation,
22-
Inject,
2322
PLATFORM_ID,
2423
NgZone,
2524
SimpleChanges,
@@ -63,7 +62,9 @@ export const DEFAULT_WIDTH = '500px';
6362
encapsulation: ViewEncapsulation.None,
6463
})
6564
export class GoogleMap implements OnChanges, OnInit, OnDestroy {
66-
private _eventManager: MapEventManager = new MapEventManager(inject(NgZone));
65+
private readonly _elementRef = inject(ElementRef);
66+
private _ngZone = inject(NgZone);
67+
private _eventManager = new MapEventManager(inject(NgZone));
6768
private _mapEl: HTMLElement;
6869
private _existingAuthFailureCallback: GoogleMapsWindow['gm_authFailure'];
6970

@@ -250,11 +251,10 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
250251
@Output() readonly zoomChanged: Observable<void> =
251252
this._eventManager.getLazyEmitter<void>('zoom_changed');
252253

253-
constructor(
254-
private readonly _elementRef: ElementRef,
255-
private _ngZone: NgZone,
256-
@Inject(PLATFORM_ID) platformId: Object,
257-
) {
254+
constructor(...args: unknown[]);
255+
256+
constructor() {
257+
const platformId = inject<Object>(PLATFORM_ID);
258258
this._isBrowser = isPlatformBrowser(platformId);
259259

260260
if (this._isBrowser) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ export const DEFAULT_MARKER_OPTIONS = {
4646
standalone: true,
4747
})
4848
export class MapAdvancedMarker implements OnInit, OnChanges, OnDestroy, MapAnchorPoint {
49+
private readonly _googleMap = inject(GoogleMap);
50+
private _ngZone = inject(NgZone);
4951
private _eventManager = new MapEventManager(inject(NgZone));
5052

5153
/**
@@ -186,10 +188,8 @@ export class MapAdvancedMarker implements OnInit, OnChanges, OnDestroy, MapAncho
186188
*/
187189
advancedMarker: google.maps.marker.AdvancedMarkerElement;
188190

189-
constructor(
190-
private readonly _googleMap: GoogleMap,
191-
private _ngZone: NgZone,
192-
) {}
191+
constructor(...args: unknown[]);
192+
constructor() {}
193193

194194
ngOnInit() {
195195
if (!this._googleMap._isBrowser) {

src/google-maps/map-base-layer.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// Workaround for: https://github.com/bazelbuild/rules_nodejs/issues/1265
1010
/// <reference types="google.maps" preserve="true" />
1111

12-
import {Directive, NgZone, OnDestroy, OnInit} from '@angular/core';
12+
import {Directive, NgZone, OnDestroy, OnInit, inject} from '@angular/core';
1313

1414
import {GoogleMap} from './google-map/google-map';
1515

@@ -19,10 +19,11 @@ import {GoogleMap} from './google-map/google-map';
1919
standalone: true,
2020
})
2121
export class MapBaseLayer implements OnInit, OnDestroy {
22-
constructor(
23-
protected readonly _map: GoogleMap,
24-
protected readonly _ngZone: NgZone,
25-
) {}
22+
protected readonly _map = inject(GoogleMap);
23+
protected readonly _ngZone = inject(NgZone);
24+
25+
constructor(...args: unknown[]);
26+
constructor() {}
2627

2728
ngOnInit() {
2829
if (this._map._isBrowser) {

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ import {MapEventManager} from '../map-event-manager';
3535
standalone: true,
3636
})
3737
export class MapCircle implements OnInit, OnDestroy {
38+
private readonly _map = inject(GoogleMap);
39+
private readonly _ngZone = inject(NgZone);
3840
private _eventManager = new MapEventManager(inject(NgZone));
3941
private readonly _options = new BehaviorSubject<google.maps.CircleOptions>({});
4042
private readonly _center = new BehaviorSubject<
4143
google.maps.LatLng | google.maps.LatLngLiteral | undefined
4244
>(undefined);
4345
private readonly _radius = new BehaviorSubject<number | undefined>(undefined);
44-
4546
private readonly _destroyed = new Subject<void>();
4647

4748
/**
@@ -161,10 +162,8 @@ export class MapCircle implements OnInit, OnDestroy {
161162
@Output() readonly circleInitialized: EventEmitter<google.maps.Circle> =
162163
new EventEmitter<google.maps.Circle>();
163164

164-
constructor(
165-
private readonly _map: GoogleMap,
166-
private readonly _ngZone: NgZone,
167-
) {}
165+
constructor(...args: unknown[]);
166+
constructor() {}
168167

169168
ngOnInit() {
170169
if (!this._map._isBrowser) {

src/google-maps/map-directions-renderer/map-directions-renderer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ import {MapEventManager} from '../map-event-manager';
3737
standalone: true,
3838
})
3939
export class MapDirectionsRenderer implements OnInit, OnChanges, OnDestroy {
40+
private readonly _googleMap = inject(GoogleMap);
41+
private _ngZone = inject(NgZone);
4042
private _eventManager = new MapEventManager(inject(NgZone));
4143

4244
/**
@@ -74,10 +76,8 @@ export class MapDirectionsRenderer implements OnInit, OnChanges, OnDestroy {
7476
/** The underlying google.maps.DirectionsRenderer object. */
7577
directionsRenderer?: google.maps.DirectionsRenderer;
7678

77-
constructor(
78-
private readonly _googleMap: GoogleMap,
79-
private _ngZone: NgZone,
80-
) {}
79+
constructor(...args: unknown[]);
80+
constructor() {}
8181

8282
ngOnInit() {
8383
if (this._googleMap._isBrowser) {

src/google-maps/map-directions-renderer/map-directions-service.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// Workaround for: https://github.com/bazelbuild/rules_nodejs/issues/1265
1010
/// <reference types="google.maps" preserve="true" />
1111

12-
import {Injectable, NgZone} from '@angular/core';
12+
import {Injectable, NgZone, inject} from '@angular/core';
1313
import {Observable} from 'rxjs';
1414

1515
export interface MapDirectionsResponse {
@@ -25,9 +25,11 @@ export interface MapDirectionsResponse {
2525
*/
2626
@Injectable({providedIn: 'root'})
2727
export class MapDirectionsService {
28+
private readonly _ngZone = inject(NgZone);
2829
private _directionsService: google.maps.DirectionsService | undefined;
2930

30-
constructor(private readonly _ngZone: NgZone) {}
31+
constructor(...args: unknown[]);
32+
constructor() {}
3133

3234
/**
3335
* See

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// Workaround for: https://github.com/bazelbuild/rules_nodejs/issues/1265
1010
/// <reference types="google.maps" preserve="true" />
1111

12-
import {Injectable, NgZone} from '@angular/core';
12+
import {Injectable, NgZone, inject} from '@angular/core';
1313
import {Observable} from 'rxjs';
1414

1515
export interface MapGeocoderResponse {
@@ -23,9 +23,11 @@ export interface MapGeocoderResponse {
2323
*/
2424
@Injectable({providedIn: 'root'})
2525
export class MapGeocoder {
26+
private readonly _ngZone = inject(NgZone);
2627
private _geocoder: google.maps.Geocoder | undefined;
2728

28-
constructor(private readonly _ngZone: NgZone) {}
29+
constructor(...args: unknown[]);
30+
constructor() {}
2931

3032
/**
3133
* See developers.google.com/maps/documentation/javascript/reference/geocoder#Geocoder.geocode

src/google-maps/map-ground-overlay/map-ground-overlay.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ import {MapEventManager} from '../map-event-manager';
3636
standalone: true,
3737
})
3838
export class MapGroundOverlay implements OnInit, OnDestroy {
39+
private readonly _map = inject(GoogleMap);
40+
private readonly _ngZone = inject(NgZone);
3941
private _eventManager = new MapEventManager(inject(NgZone));
40-
4142
private readonly _opacity = new BehaviorSubject<number>(1);
4243
private readonly _url = new BehaviorSubject<string>('');
4344
private readonly _bounds = new BehaviorSubject<
@@ -96,10 +97,8 @@ export class MapGroundOverlay implements OnInit, OnDestroy {
9697
@Output() readonly groundOverlayInitialized: EventEmitter<google.maps.GroundOverlay> =
9798
new EventEmitter<google.maps.GroundOverlay>();
9899

99-
constructor(
100-
private readonly _map: GoogleMap,
101-
private readonly _ngZone: NgZone,
102-
) {}
100+
constructor(...args: unknown[]);
101+
constructor() {}
103102

104103
ngOnInit() {
105104
if (this._map._isBrowser) {

src/google-maps/map-heatmap-layer/map-heatmap-layer.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
SimpleChanges,
2020
Output,
2121
EventEmitter,
22+
inject,
2223
} from '@angular/core';
2324

2425
import {GoogleMap} from '../google-map/google-map';
@@ -41,6 +42,9 @@ export type HeatmapData =
4142
standalone: true,
4243
})
4344
export class MapHeatmapLayer implements OnInit, OnChanges, OnDestroy {
45+
private readonly _googleMap = inject(GoogleMap);
46+
private _ngZone = inject(NgZone);
47+
4448
/**
4549
* Data shown on the heatmap.
4650
* See: https://developers.google.com/maps/documentation/javascript/reference/visualization
@@ -72,10 +76,8 @@ export class MapHeatmapLayer implements OnInit, OnChanges, OnDestroy {
7276
@Output() readonly heatmapInitialized: EventEmitter<google.maps.visualization.HeatmapLayer> =
7377
new EventEmitter<google.maps.visualization.HeatmapLayer>();
7478

75-
constructor(
76-
private readonly _googleMap: GoogleMap,
77-
private _ngZone: NgZone,
78-
) {}
79+
constructor(...args: unknown[]);
80+
constructor() {}
7981

8082
ngOnInit() {
8183
if (this._googleMap._isBrowser) {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ import {MapAnchorPoint} from '../map-anchor-point';
3939
host: {'style': 'display: none'},
4040
})
4141
export class MapInfoWindow implements OnInit, OnDestroy {
42+
private readonly _googleMap = inject(GoogleMap);
43+
private _elementRef = inject<ElementRef<HTMLElement>>(ElementRef);
44+
private _ngZone = inject(NgZone);
4245
private _eventManager = new MapEventManager(inject(NgZone));
4346
private readonly _options = new BehaviorSubject<google.maps.InfoWindowOptions>({});
4447
private readonly _position = new BehaviorSubject<
@@ -105,11 +108,8 @@ export class MapInfoWindow implements OnInit, OnDestroy {
105108
@Output() readonly infoWindowInitialized: EventEmitter<google.maps.InfoWindow> =
106109
new EventEmitter<google.maps.InfoWindow>();
107110

108-
constructor(
109-
private readonly _googleMap: GoogleMap,
110-
private _elementRef: ElementRef<HTMLElement>,
111-
private _ngZone: NgZone,
112-
) {}
111+
constructor(...args: unknown[]);
112+
constructor() {}
113113

114114
ngOnInit() {
115115
if (this._googleMap._isBrowser) {

0 commit comments

Comments
 (0)