angular-google-maps icon indicating copy to clipboard operation
angular-google-maps copied to clipboard

Close marker info windows when a new one clicked?

Open SteveDowsett opened this issue 9 years ago • 23 comments

Is is possible to do this?

SteveDowsett avatar Dec 19 '16 15:12 SteveDowsett

Yup, I've encountered the same issue before.

Here's the working plunk http://plnkr.co/edit/cwuwBN?p=preview

lauwrentius avatar Dec 22 '16 17:12 lauwrentius

i could not use this approach. My solution was downloaded the sources and include the following code in the file (google-map-info-window.ts):

// set this property in the top file, below let infoWindowId = 0; (line 7) let infoWindowLastContext: SebmGoogleMapInfoWindow = null;

open(): Promise { if(infoWindowLastContext !== null) { this._infoWindowManager.close(infoWindowLastContext) }

infoWindowLastContext = this;

return this._infoWindowManager.open(this); 

}

I hope this helps...

lsnascimento avatar Dec 29 '16 16:12 lsnascimento

My template only solution:

<sebm-google-map #gm [latitude]="lat" [longitude]="lng" [zoom]="zoom" [scrollwheel]="false" [styles]="mapStyles">
	<sebm-google-map-marker *ngFor="let place of places"
	[latitude]="place.point.lat"
	[longitude]="place.point.lng"
	[label]="place.name[0]"
	(markerClick)="gm.lastOpen?.close(); gm.lastOpen = infoWindow">
	<sebm-google-map-info-window #infoWindow>
		<strong>{{place.name}}</strong>
	</sebm-google-map-info-window>
</sebm-google-map-marker>

soyersoyer avatar Feb 23 '17 22:02 soyersoyer

@soyersoyer @lazarljubenovic

<agm-map #gm [latitude]="lat" [longitude]="lng" [zoom]="zoom" [scrollwheel]="false" [styles]="mapStyles">
	<sebm-google-map-marker *ngFor="let place of places"
	[latitude]="place.point.lat"
	[longitude]="place.point.lng"
	[label]="place.name[0]"
	(markerClick)="gm.lastOpen?.close(); gm.lastOpen = infoWindow">
	<agm-info-window #infoWindow>
		<strong>{{place.name}}</strong>
	</agm-info-window>
</agm-marker>

With the new 1.0.0-beta.0 - green-zebra AOT is not working: Property 'lastOpen' does not exist on type 'AgmMap'

What can be happen?

ekaitzht avatar Apr 26 '17 15:04 ekaitzht

Works great, my template usage invokes a custom click function as in

(markerClick)="gm.lastOpen?.close(); gm.lastOpen = infoWindow; markerClicked(m);"

Martin-B avatar May 04 '17 15:05 Martin-B

Hello,

Even with @Martin-B solution, it doesn't work :/

Any help ?

melbouhy avatar Jun 14 '17 10:06 melbouhy

@Martin-B thanks heaps for the solution. Works a treat!

rubyboy avatar Jul 13 '17 13:07 rubyboy

@Martin-B Nice thx ! @melbouhy Shame on me; forgot to add #infoWindow in <agm-info-window>. Maybe you did the same mistake ?

Eirenempada avatar Jul 17 '17 19:07 Eirenempada

<agm-marker (markerClick)="openIW(infoWindow)"> <agm-info-window #infoWindow>

public openIW(data = undefined){ if(this.IW){ this.IW.close(); }

this.IW = data;

}

sergamers avatar Sep 18 '17 09:09 sergamers

@Martin-B how to use this ((markerClick)="gm.lastOpen?.close(); gm.lastOpen = infoWindow; markerClicked(m);") in agm-marker please reply with .ts code

menakav avatar Dec 08 '17 10:12 menakav

@menakav Here is a worked code: in .ts file: ` markerClicked(infoWindow) { if( this.infoWindowOpened === infoWindow) return;

    if(this.infoWindowOpened !== null)
    {
        this.infoWindowOpened.close();
    }

    this.infoWindowOpened = infoWindow;
}

in .html file: (markerClick)="gm.lastOpen?.close(); gm.lastOpen = infoWindow; markerClicked(m);"`

AzimbekMakhmudov avatar Jan 19 '18 06:01 AzimbekMakhmudov

@menakav Your code don't works for me :(

I tested all the codes in this forum, and my problem is the infoWindow received here:

  clickedMarker(infoWindow, index: number) {
    ...
  }

infoWindow always is the first one, no matter how any points I have. Always I received the infoWindow with _id = 0.

The parameter index receives the true ID of the clicked marker....

This is my html:

<agm-marker *ngFor="let point of points; let i = index" [latitude]="point.latitude"
                            [longitude]="point.longitude"
                            [iconUrl]="iconUrl" (markerClick)="clickedMarker(infoWindow, i)">
<agm-info-window [disableAutoPan]="true" #infoWindow *ngIf="!isDetailMovil">
        <div class="marker__tittle">{{point.name}}</div>

Any ideas why always is coming the same infoWindow?

gabrielizalo avatar Jan 23 '18 16:01 gabrielizalo

@soyersoyer 's "template only" solution works great. However, once the initial agm-marker array is replaced with new marker elements (e.g. after ajax update), all of a sudden, the previous infowindows don't close automatically anymore. What could be the problem here?

markschmid avatar Jan 30 '18 08:01 markschmid

@markschmid my workaround when the initial agm-marker array is replaced. (markerClick)="tmp=gm.lastOpen;gm.lastOpen=infoWindow;tmp?.close();"

songsq avatar Mar 29 '18 06:03 songsq

My solution, based on @lauwrentius answer:

*.html

<agm-marker 
    *ngFor="let marker of markers; let i = index"
    (markerClick)="showInfoWindow(infoWindow, i)"
>
    <agm-info-window #infoWindow></agm-info-window>
</agm-marker>

*.ts

infoWindowOpened = null;

filter() {
    infoWindowOpened = null;
    // redraw the map with filtered markers
}

showInfoWindow(infoWindow, index) {
    if (this.infoWindowOpened === infoWindow) {
        return;
    }

    if (this.infoWindowOpened !== null) {
        this.infoWindowOpened.close();
    }
    
    this.infoWindowOpened = infoWindow;   
}

JimmyTheNerd avatar Apr 08 '18 13:04 JimmyTheNerd

@JimmyTheNerd

Can you explain how you are running filter(){infoWindowOpened = null;} without it throwing a 'cannot find infoWindowOpened' as an error? This occurs after changing the marker icon array.

Antditto avatar Apr 25 '18 17:04 Antditto

I solved the problem. I shared the code: <agm-marker *ngFor="let point of points; let i = index" [latitude]="point.latitude" [longitude]="point.longitude" [iconUrl]="iconUrl" (markerClick)="clickedMarker(infoWindow, point, point.latitude, point.longitude)"> <agm-info-window #infoWindow [disableAutoPan]="true" *ngIf="!isDetailMovil">

clickedMarker(data = undefined, currentPoint: any, lat: string, lng: string) { if (this.IW) { this.IW.close(); } const details = data._infoWindowManager._infoWindows; if (details.size > 0) { details.forEach((element, key) => { if (key.hostMarker.latitude === lat && key.hostMarker.longitude === lng) { this.IW = key; } }); } }

i Hope it help.

CristianHCol avatar Apr 27 '18 20:04 CristianHCol

Thank you.

On Sat, 28 Apr 2018 at 02:04, Cristian Hernandez [email protected] wrote:

I solved the problem. I shared the code: <agm-marker *ngFor="let point of points; let i = index" [latitude]="point.latitude" [longitude]="point.longitude" [iconUrl]="iconUrl" (markerClick)="clickedMarker(infoWindow, point, point.latitude, point.longitude)"> <agm-info-window #infoWindow [disableAutoPan]="true" *ngIf="!isDetailMovil">

clickedMarker(data = undefined, currentPoint: any, lat: string, lng: string) { if (this.IW) { this.IW.close(); } const details = data._infoWindowManager._infoWindows; if (details.size > 0) { details.forEach((element, key) => { if (key.hostMarker.latitude === lat && key.hostMarker.longitude === lng) { this.IW = key; } }); } }

i Hope it help.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/SebastianM/angular-google-maps/issues/797#issuecomment-385086695, or mute the thread https://github.com/notifications/unsubscribe-auth/AgIM0TGCgyHK-snaJdIlSZSQuq0MF1Ihks5ts4DDgaJpZM4LQ0M7 .

menakav avatar Apr 28 '18 07:04 menakav

Here's my solution. Works with angular 6. HTML:

<agm-marker *ngFor="let marker of markers" [latitude]="marker.latitude"[longitude]="marker.longitude" (markerClick)="markerClick(infoWindow)">
<agm-info-window #infoWindow>
</agm-info-window>
</agm-marker>

TypeScript:

lastSelectedInfoWindow: any;
markerClick(infoWindow: any) {
        if (infoWindow == this.lastSelectedInfoWindow) {
            return;
        }
        if (this.lastSelectedInfoWindow != null) {
         try{
            this.lastSelectedInfoWindow.close();
          } catch {} //in case if you reload your markers
        }
        this.lastSelectedInfoWindow = infoWindow;
    }

kwiniarski97 avatar Jul 10 '18 08:07 kwiniarski97

After a long research and unclear solutions a finally got a fine solution for clear previous agm-info-window when a new one is opened

This is my component.html:

 <agm-map (mapClick)="close_window($event)" [fullscreenControl]='true' [mapTypeControl]='true' [latitude]="lat" [longitude]="lng" [zoom]="13" [scrollwheel]="false">
     <div *ngFor="let data of zonas">
        <agm-marker (markerClick)=select_marker(infoWindow) [latitude]="data.latitud" [longitude]="data.longitud">
           <agm-info-window #infoWindow >            
           </agm-info-window>
         </agm-marker>
      </div>
  </agm-map>

And this is my component.ts:

constructor(public apiService: ApiService) {}
infoWindowOpened = null
previous_info_window = null
...
close_window(){
if (this.previous_info_window != null ) {
  this.previous_info_window.close()
  }    
}

select_marker(data,infoWindow){
 if (this.previous_info_window == null)
  this.previous_info_window = infoWindow;
 else{
  this.infoWindowOpened = infoWindow
  this.previous_info_window.close()
 }
 this.previous_info_window = infoWindow
}

So every time a new window will be open it detect the event and closes the previous one, also this is working for closing a opened window if the user clicks outside the window in any other part of the map

gabolucuy avatar Sep 03 '18 23:09 gabolucuy