I am using Angular google map: https://angular-maps.com/.
Right now as default functionality, it open the info-window defined inside marker.
And on click of any new marker it remains open old window. I need to close info window on click of another marker.
This is html code:
<agm-map [latitude]="lat" [longitude]="lng" [zoom]="zoom">
<agm-marker
*ngFor="let m of markers; let i = index"
(markerClick)="clickedMarker(m.label, infoWindow, i)"
[latitude]="m.lat"
[longitude]="m.lng"
[markerDraggable]="m.draggable"
[iconUrl]="m.icon">
<agm-info-window #infoWindow>
<strong>{{m.name}}</strong><br>
</agm-info-window>
</agm-marker>
</agm-map>
this is code when marker is clicked:
clickedMarker(label: string, infoWindow, marker, index: number) {
if( this.infoWindowOpened === infoWindow)
return;
if(this.infoWindowOpened !== null)
{
this.infoWindowOpened.close();
}
this.infoWindowOpened = infoWindow;
}
It works when the page refresh. But as soon as I refresh markers variable it show this error:
ERROR TypeError: Cannot read property 'then' of undefined
at InfoWindowManager.close (info-window-manager.js:48)
at AgmInfoWindow.close (info-window.js:94)
at SearchComponent.clickedMarker (search.component.ts:188)
component code to refresh markers:
this.markers.push({
name: item.Name,
lat: parseFloat(item.latitude.toString()),
lng: parseFloat(item.longitude.toString()),
draggable: false,
icon:'assets/img/marker.png'
});
Best Answer
You can rewrite your
clickedMarker
very simple as followsMake the change to your event binding function in the template
The live code is here