Skip to content

Commit

Permalink
feat(google-maps): Getting google.maps.Map instance (#23856)
Browse files Browse the repository at this point in the history
Added mapInitialized event which is emitted when the map is initialized and returns the map instance.

For #23703
  • Loading branch information
umernaeem217 committed Nov 8, 2021
1 parent ff7fd48 commit 7dbe9a4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/google-maps/google-map/google-map.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,18 @@ describe('GoogleMap', () => {
expect(mapConstructorSpy.calls.mostRecent()?.args[1].mapTypeId).toBe('satellite');
});

it('should emit mapInitialized event when the map is initialized', () => {
mapSpy = createMapSpy(DEFAULT_OPTIONS);
mapConstructorSpy = createMapConstructorSpy(mapSpy);

const fixture = TestBed.createComponent(TestApp);
fixture.detectChanges();

expect(fixture.componentInstance.mapInitializedSpy).toHaveBeenCalledOnceWith(
fixture.componentInstance.map.googleMap,
);
});

it('should emit authFailure event when window.gm_authFailure is called', () => {
mapSpy = createMapSpy(DEFAULT_OPTIONS);
mapConstructorSpy = createMapConstructorSpy(mapSpy);
Expand Down Expand Up @@ -397,7 +409,8 @@ describe('GoogleMap', () => {
[mapTypeId]="mapTypeId"
(mapClick)="handleClick($event)"
(centerChanged)="handleCenterChanged()"
(mapRightclick)="handleRightclick($event)">
(mapRightclick)="handleRightclick($event)"
(mapInitialized)="mapInitializedSpy($event)">
</google-map>`,
})
class TestApp {
Expand All @@ -412,4 +425,5 @@ class TestApp {
handleClick(event: google.maps.MapMouseEvent) {}
handleCenterChanged() {}
handleRightclick(event: google.maps.MapMouseEvent) {}
mapInitializedSpy = jasmine.createSpy('mapInitialized');
}
5 changes: 5 additions & 0 deletions src/google-maps/google-map/google-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
}
private _options = DEFAULT_OPTIONS;

/** Event emitted when the map is initialized. */
@Output() readonly mapInitialized: EventEmitter<google.maps.Map> =
new EventEmitter<google.maps.Map>();

/**
* See
* https://developers.google.com/maps/documentation/javascript/events#auth-errors
Expand Down Expand Up @@ -305,6 +309,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
this.googleMap = new google.maps.Map(this._mapEl, this._combineOptions());
});
this._eventManager.setTarget(this.googleMap);
this.mapInitialized.emit(this.googleMap);
}
}

Expand Down
3 changes: 2 additions & 1 deletion tools/public_api_guard/google-maps/google-maps.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
readonly mapDrag: Observable<void>;
readonly mapDragend: Observable<void>;
readonly mapDragstart: Observable<void>;
readonly mapInitialized: EventEmitter<google.maps.Map>;
readonly mapMousemove: Observable<google.maps.MapMouseEvent>;
readonly mapMouseout: Observable<google.maps.MapMouseEvent>;
readonly mapMouseover: Observable<google.maps.MapMouseEvent>;
Expand Down Expand Up @@ -92,7 +93,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
set zoom(zoom: number);
readonly zoomChanged: Observable<void>;
// (undocumented)
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, ["*"]>;
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, ["*"]>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<GoogleMap, never>;
}
Expand Down

0 comments on commit 7dbe9a4

Please sign in to comment.