Skip to content

Commit 309a6c3

Browse files
committed
feat(google-maps): add event emitter for gm_authFailure callback
1 parent 27f5ca9 commit 309a6c3

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

src/dev-app/google-map/google-map-demo.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
width="750px"
44
[center]="center"
55
[zoom]="zoom"
6+
(authFailure)="authFailure()"
67
(mapClick)="handleClick($event)"
78
(mapMousemove)="handleMove($event)"
89
(mapRightclick)="handleRightclick()"

src/dev-app/google-map/google-map-demo.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ export class GoogleMapDemo {
107107

108108
constructor(private readonly _mapDirectionsService: MapDirectionsService) {}
109109

110+
authFailure() {
111+
console.log('Auth failure event emitted');
112+
}
113+
110114
handleClick(event: google.maps.MapMouseEvent) {
111115
this.markerPositions.push(event.latLng.toJSON());
112116
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ describe('GoogleMap', () => {
3939

4040
afterEach(() => {
4141
(window.google as any) = undefined;
42+
(window as any).gm_authFailure = undefined;
4243
});
4344

4445
it('throws an error is the Google Maps JavaScript API was not loaded', () => {
@@ -358,6 +359,21 @@ describe('GoogleMap', () => {
358359
expect(mapConstructorSpy.calls.mostRecent()?.args[1].mapTypeId).toBe('satellite');
359360
});
360361

362+
it('should emit authFailure event when window.gm_authFailure is called', () => {
363+
mapSpy = createMapSpy(DEFAULT_OPTIONS);
364+
mapConstructorSpy = createMapConstructorSpy(mapSpy);
365+
366+
expect((window as any).gm_authFailure).toBeUndefined();
367+
368+
const fixture = TestBed.createComponent(TestApp);
369+
fixture.detectChanges();
370+
spyOn(fixture.componentInstance.map.authFailure, 'emit');
371+
372+
expect((window as any).gm_authFailure).toBeDefined();
373+
(window as any).gm_authFailure();
374+
375+
expect(fixture.componentInstance.map.authFailure.emit).toHaveBeenCalled();
376+
});
361377
});
362378

363379
@Component({

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ import {
2323
PLATFORM_ID,
2424
NgZone,
2525
SimpleChanges,
26+
EventEmitter,
2627
} from '@angular/core';
2728
import {isPlatformBrowser} from '@angular/common';
2829
import {Observable} from 'rxjs';
2930
import {MapEventManager} from '../map-event-manager';
3031

3132
interface GoogleMapsWindow extends Window {
33+
gm_authFailure?: () => void;
3234
google?: typeof google;
3335
}
3436

@@ -101,6 +103,12 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
101103
}
102104
private _options = DEFAULT_OPTIONS;
103105

106+
/**
107+
* See
108+
* https://developers.google.com/maps/documentation/javascript/events#auth-errors
109+
*/
110+
@Output() readonly authFailure: EventEmitter<void> = new EventEmitter<void>();
111+
104112
/**
105113
* See
106114
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.bounds_changed
@@ -245,6 +253,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
245253
'https://developers.google.com/maps/documentation/javascript/' +
246254
'tutorial#Loading_the_Maps_API');
247255
}
256+
googleMapsWindow.gm_authFailure = () => this.authFailure.emit();
248257
}
249258
}
250259

0 commit comments

Comments
 (0)