Skip to content

Commit 7dbe9a4

Browse files
authored
feat(google-maps): Getting google.maps.Map instance (#23856)
Added mapInitialized event which is emitted when the map is initialized and returns the map instance. For #23703
1 parent ff7fd48 commit 7dbe9a4

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,18 @@ describe('GoogleMap', () => {
363363
expect(mapConstructorSpy.calls.mostRecent()?.args[1].mapTypeId).toBe('satellite');
364364
});
365365

366+
it('should emit mapInitialized event when the map is initialized', () => {
367+
mapSpy = createMapSpy(DEFAULT_OPTIONS);
368+
mapConstructorSpy = createMapConstructorSpy(mapSpy);
369+
370+
const fixture = TestBed.createComponent(TestApp);
371+
fixture.detectChanges();
372+
373+
expect(fixture.componentInstance.mapInitializedSpy).toHaveBeenCalledOnceWith(
374+
fixture.componentInstance.map.googleMap,
375+
);
376+
});
377+
366378
it('should emit authFailure event when window.gm_authFailure is called', () => {
367379
mapSpy = createMapSpy(DEFAULT_OPTIONS);
368380
mapConstructorSpy = createMapConstructorSpy(mapSpy);
@@ -397,7 +409,8 @@ describe('GoogleMap', () => {
397409
[mapTypeId]="mapTypeId"
398410
(mapClick)="handleClick($event)"
399411
(centerChanged)="handleCenterChanged()"
400-
(mapRightclick)="handleRightclick($event)">
412+
(mapRightclick)="handleRightclick($event)"
413+
(mapInitialized)="mapInitializedSpy($event)">
401414
</google-map>`,
402415
})
403416
class TestApp {
@@ -412,4 +425,5 @@ class TestApp {
412425
handleClick(event: google.maps.MapMouseEvent) {}
413426
handleCenterChanged() {}
414427
handleRightclick(event: google.maps.MapMouseEvent) {}
428+
mapInitializedSpy = jasmine.createSpy('mapInitialized');
415429
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
104104
}
105105
private _options = DEFAULT_OPTIONS;
106106

107+
/** Event emitted when the map is initialized. */
108+
@Output() readonly mapInitialized: EventEmitter<google.maps.Map> =
109+
new EventEmitter<google.maps.Map>();
110+
107111
/**
108112
* See
109113
* https://developers.google.com/maps/documentation/javascript/events#auth-errors
@@ -305,6 +309,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
305309
this.googleMap = new google.maps.Map(this._mapEl, this._combineOptions());
306310
});
307311
this._eventManager.setTarget(this.googleMap);
312+
this.mapInitialized.emit(this.googleMap);
308313
}
309314
}
310315

tools/public_api_guard/google-maps/google-maps.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
6565
readonly mapDrag: Observable<void>;
6666
readonly mapDragend: Observable<void>;
6767
readonly mapDragstart: Observable<void>;
68+
readonly mapInitialized: EventEmitter<google.maps.Map>;
6869
readonly mapMousemove: Observable<google.maps.MapMouseEvent>;
6970
readonly mapMouseout: Observable<google.maps.MapMouseEvent>;
7071
readonly mapMouseover: Observable<google.maps.MapMouseEvent>;
@@ -92,7 +93,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
9293
set zoom(zoom: number);
9394
readonly zoomChanged: Observable<void>;
9495
// (undocumented)
95-
static ɵcmp: i0.ɵɵComponentDeclaration<GoogleMap, "google-map", ["googleMap"], { "height": "height"; "width": "width"; "mapTypeId": "mapTypeId"; "center": "center"; "zoom": "zoom"; "options": "options"; }, { "authFailure": "authFailure"; "boundsChanged": "boundsChanged"; "centerChanged": "centerChanged"; "mapClick": "mapClick"; "mapDblclick": "mapDblclick"; "mapDrag": "mapDrag"; "mapDragend": "mapDragend"; "mapDragstart": "mapDragstart"; "headingChanged": "headingChanged"; "idle": "idle"; "maptypeidChanged": "maptypeidChanged"; "mapMousemove": "mapMousemove"; "mapMouseout": "mapMouseout"; "mapMouseover": "mapMouseover"; "projectionChanged": "projectionChanged"; "mapRightclick": "mapRightclick"; "tilesloaded": "tilesloaded"; "tiltChanged": "tiltChanged"; "zoomChanged": "zoomChanged"; }, never, ["*"]>;
96+
static ɵcmp: i0.ɵɵComponentDeclaration<GoogleMap, "google-map", ["googleMap"], { "height": "height"; "width": "width"; "mapTypeId": "mapTypeId"; "center": "center"; "zoom": "zoom"; "options": "options"; }, { "mapInitialized": "mapInitialized"; "authFailure": "authFailure"; "boundsChanged": "boundsChanged"; "centerChanged": "centerChanged"; "mapClick": "mapClick"; "mapDblclick": "mapDblclick"; "mapDrag": "mapDrag"; "mapDragend": "mapDragend"; "mapDragstart": "mapDragstart"; "headingChanged": "headingChanged"; "idle": "idle"; "maptypeidChanged": "maptypeidChanged"; "mapMousemove": "mapMousemove"; "mapMouseout": "mapMouseout"; "mapMouseover": "mapMouseover"; "projectionChanged": "projectionChanged"; "mapRightclick": "mapRightclick"; "tilesloaded": "tilesloaded"; "tiltChanged": "tiltChanged"; "zoomChanged": "zoomChanged"; }, never, ["*"]>;
9697
// (undocumented)
9798
static ɵfac: i0.ɵɵFactoryDeclaration<GoogleMap, never>;
9899
}

0 commit comments

Comments
 (0)