From c5a24141553eac074f0646444edf80b79a079709 Mon Sep 17 00:00:00 2001 From: svarzee Date: Mon, 21 Nov 2016 15:19:21 +0100 Subject: [PATCH] fix(marker): fix mislocated info window Fixes the problem with mislocated info window after being removed and recreated with *ngIf Fixes #752 Closes #754 --- src/core/directives/google-map-marker.ts | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/core/directives/google-map-marker.ts b/src/core/directives/google-map-marker.ts index bc475f9be..109635c8c 100644 --- a/src/core/directives/google-map-marker.ts +++ b/src/core/directives/google-map-marker.ts @@ -1,4 +1,5 @@ -import {AfterContentInit, ContentChild, Directive, EventEmitter, OnChanges, OnDestroy, SimpleChange} from '@angular/core'; +import {Directive, EventEmitter, OnChanges, OnDestroy, SimpleChange, + AfterContentInit, ContentChildren, QueryList} from '@angular/core'; import {Subscription} from 'rxjs/Subscription'; import {MouseEvent} from '../map-types'; @@ -119,7 +120,7 @@ export class SebmGoogleMapMarker implements OnDestroy, OnChanges, AfterContentIn /** * @internal */ - @ContentChild(SebmGoogleMapInfoWindow) infoWindow: SebmGoogleMapInfoWindow; + @ContentChildren(SebmGoogleMapInfoWindow) infoWindow: QueryList = new QueryList(); private _markerAddedToManger: boolean = false; private _id: string; @@ -129,9 +130,17 @@ export class SebmGoogleMapMarker implements OnDestroy, OnChanges, AfterContentIn /* @internal */ ngAfterContentInit() { - if (this.infoWindow != null) { - this.infoWindow.hostMarker = this; + this.handleInfoWindowUpdate(); + this.infoWindow.changes.subscribe(() => this.handleInfoWindowUpdate()); + } + + private handleInfoWindowUpdate() { + if (this.infoWindow.length > 1) { + throw new Error('Expected no more than one info window.'); } + this.infoWindow.forEach(marker => { + marker.hostMarker = this; + }); } /** @internal */ @@ -173,8 +182,8 @@ export class SebmGoogleMapMarker implements OnDestroy, OnChanges, AfterContentIn private _addEventListeners() { const cs = this._markerManager.createEventObservable('click', this).subscribe(() => { - if (this.openInfoWindow && this.infoWindow != null) { - this.infoWindow.open(); + if (this.openInfoWindow) { + this.infoWindow.forEach(infoWindow => infoWindow.open()); } this.markerClick.emit(null); });