Skip to content

Commit 4284b2e

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 14a51ef commit 4284b2e

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-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
@@ -128,6 +128,28 @@ describe('MapInfoWindow', () => {
128128
expect(infoWindowSpy.open).toHaveBeenCalledWith(mapSpy, fakeMarker);
129129
});
130130

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

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,12 @@ export class MapInfoWindow implements OnInit, OnDestroy {
173173
open(anchor?: MapMarker) {
174174
this._assertInitialized();
175175
const marker = anchor ? anchor.marker : undefined;
176-
this._elementRef.nativeElement.style.display = '';
177-
this.infoWindow.open(this._googleMap.googleMap, marker);
176+
177+
// Prevent the info window from initializing if trying to reopen on the same marker.
178+
if (this.infoWindow.get('anchor') !== marker) {
179+
this._elementRef.nativeElement.style.display = '';
180+
this.infoWindow.open(this._googleMap.googleMap, marker);
181+
}
178182
}
179183

180184
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
@@ -86,10 +86,14 @@ export function createMarkerConstructorSpy(markerSpy: jasmine.SpyObj<google.maps
8686
/** Creates a jasmine.SpyObj for a google.maps.InfoWindow */
8787
export function createInfoWindowSpy(options: google.maps.InfoWindowOptions):
8888
jasmine.SpyObj<google.maps.InfoWindow> {
89+
let anchor: any;
8990
const infoWindowSpy = jasmine.createSpyObj(
9091
'google.maps.InfoWindow',
91-
['addListener', 'close', 'getContent', 'getPosition', 'getZIndex', 'open']);
92+
['addListener', 'close', 'getContent', 'getPosition', 'getZIndex', 'open', 'get']);
9293
infoWindowSpy.addListener.and.returnValue({remove: () => {}});
94+
infoWindowSpy.open.and.callFake((_map: any, target: any) => anchor = target);
95+
infoWindowSpy.close.and.callFake(() => anchor = null);
96+
infoWindowSpy.get.and.callFake((key: string) => key === 'anchor' ? anchor : null);
9397
return infoWindowSpy;
9498
}
9599

0 commit comments

Comments
 (0)