Skip to content

Commit d62c70d

Browse files
crisbetojelbourn
authored andcommitted
fix(google-maps): avoid re-initializing info window for same marker (#19299)
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. (cherry picked from commit 90f4d0e)
1 parent 71a2a17 commit d62c70d

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,31 @@ 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 = {
135+
marker: fakeMarker,
136+
getAnchor: () => fakeMarker
137+
} as unknown as MapMarker;
138+
const infoWindowSpy = createInfoWindowSpy({});
139+
createInfoWindowConstructorSpy(infoWindowSpy).and.callThrough();
140+
141+
const fixture = TestBed.createComponent(TestApp);
142+
const infoWindowComponent = fixture.debugElement.query(By.directive(
143+
MapInfoWindow))!.injector.get<MapInfoWindow>(MapInfoWindow);
144+
fixture.detectChanges();
145+
146+
infoWindowComponent.open(fakeMarkerComponent);
147+
expect(infoWindowSpy.open).toHaveBeenCalledTimes(1);
148+
149+
infoWindowComponent.open(fakeMarkerComponent);
150+
expect(infoWindowSpy.open).toHaveBeenCalledTimes(1);
151+
152+
infoWindowComponent.close();
153+
infoWindowComponent.open(fakeMarkerComponent);
154+
expect(infoWindowSpy.open).toHaveBeenCalledTimes(2);
155+
});
156+
132157
it('exposes methods that provide information about the info window', () => {
133158
const infoWindowSpy = createInfoWindowSpy({});
134159
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
@@ -173,8 +173,13 @@ export class MapInfoWindow implements OnInit, OnDestroy {
173173
*/
174174
open(anchor?: MapAnchorPoint) {
175175
this._assertInitialized();
176-
this._elementRef.nativeElement.style.display = '';
177-
this.infoWindow.open(this._googleMap.googleMap, anchor ? anchor.getAnchor() : undefined);
176+
const anchorObject = anchor ? anchor.getAnchor() : undefined;
177+
178+
// Prevent the info window from initializing if trying to reopen on the same anchor.
179+
if (this.infoWindow.get('anchor') !== anchorObject) {
180+
this._elementRef.nativeElement.style.display = '';
181+
this.infoWindow.open(this._googleMap.googleMap, anchorObject);
182+
}
178183
}
179184

180185
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)