Skip to content

Commit 3b5c2bf

Browse files
committed
fix(google-maps): avoid re-initializing info window for same marker
Currently if `open` is called multiple times for the same object, we keep re-initializing the info window. This can be seen in the dev app, because Chrome keeps logging warnings for some event listeners. These changes add a check that keeps the existing info window.
1 parent 5f12539 commit 3b5c2bf

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,28 @@ describe('MapInfoWindow', () => {
129129
expect(infoWindowSpy.open).toHaveBeenCalledWith(mapSpy, fakeMarker);
130130
});
131131

132+
it('should not try to reopen info window multiple times for the same marker', () => {
133+
const fakeMarker = {} as unknown as google.maps.Marker;
134+
const fakeMarkerComponent = {marker: fakeMarker} as unknown as MapMarker;
135+
const infoWindowSpy = createInfoWindowSpy({});
136+
createInfoWindowConstructorSpy(infoWindowSpy).and.callThrough();
137+
138+
const fixture = TestBed.createComponent(TestApp);
139+
const infoWindowComponent = fixture.debugElement.query(By.directive(
140+
MapInfoWindow))!.injector.get<MapInfoWindow>(MapInfoWindow);
141+
fixture.detectChanges();
142+
143+
infoWindowComponent.open(fakeMarkerComponent);
144+
expect(infoWindowSpy.open).toHaveBeenCalledTimes(1);
145+
146+
infoWindowComponent.open(fakeMarkerComponent);
147+
expect(infoWindowSpy.open).toHaveBeenCalledTimes(1);
148+
149+
infoWindowComponent.close();
150+
infoWindowComponent.open(fakeMarkerComponent);
151+
expect(infoWindowSpy.open).toHaveBeenCalledTimes(2);
152+
});
153+
132154
it('exposes methods that provide information about the info window', () => {
133155
const infoWindowSpy = createInfoWindowSpy({});
134156
createInfoWindowConstructorSpy(infoWindowSpy).and.callThrough();

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,13 @@ export class MapInfoWindow implements OnInit, OnDestroy {
172172
*/
173173
open(anchor?: MapAnchorPoint) {
174174
this._assertInitialized();
175-
this._elementRef.nativeElement.style.display = '';
176-
this.infoWindow.open(this._googleMap.googleMap, anchor ? anchor.getAnchor() : undefined);
175+
const anchorObject = anchor ? anchor.getAnchor() : undefined;
176+
177+
// Prevent the info window from initializing if trying to reopen on the same anchor.
178+
if (this.infoWindow.get('anchor') !== anchorObject) {
179+
this._elementRef.nativeElement.style.display = '';
180+
this.infoWindow.open(this._googleMap.googleMap, anchorObject);
181+
}
177182
}
178183

179184
private _combineOptions(): Observable<google.maps.InfoWindowOptions> {

src/google-maps/testing/fake-google-map-utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,14 @@ export function createMarkerConstructorSpy(markerSpy: jasmine.SpyObj<google.maps
9292
/** Creates a jasmine.SpyObj for a google.maps.InfoWindow */
9393
export function createInfoWindowSpy(options: google.maps.InfoWindowOptions):
9494
jasmine.SpyObj<google.maps.InfoWindow> {
95+
let anchor: any;
9596
const infoWindowSpy = jasmine.createSpyObj(
9697
'google.maps.InfoWindow',
97-
['addListener', 'close', 'getContent', 'getPosition', 'getZIndex', 'open']);
98+
['addListener', 'close', 'getContent', 'getPosition', 'getZIndex', 'open', 'get']);
9899
infoWindowSpy.addListener.and.returnValue({remove: () => {}});
100+
infoWindowSpy.open.and.callFake((_map: any, target: any) => anchor = target);
101+
infoWindowSpy.close.and.callFake(() => anchor = null);
102+
infoWindowSpy.get.and.callFake((key: string) => key === 'anchor' ? anchor : null);
99103
return infoWindowSpy;
100104
}
101105

0 commit comments

Comments
 (0)