How to change icon of previously clicked marker in angular google maps
Asked Answered
R

1

-2

I managed to change the icon of currently clicked marker using the below code. I have multiple markers on page. Now the issue is , if i click the second marker, the icon of the previously clicked marker should be changed to its original one (inactive.png) and the icon of currently clicked marker should use (active.png).How can I achieve this? Please help.

In the below code if m.isClicked is true, then inactive.png is used, else active.png is used.

<agm-marker *ngFor="let m of mapArrayList" (markerClick)="clickedMarker(infowindow, m)"
    [latitude]="m.geometry.location.lat()" [longitude]="m.geometry.location.lng()" 
    [iconUrl] ="
      {
        url: m.isClicked ? './assets/images/marker_inactive.png' : './assets/images/marker_active.png',
        scaledSize: {
            width: 40,
            height: 60
        }
    }">




 clickedMarker(infowindow, m) {
        m.isClicked = false;   // once the marker is clicked, the icon of marker changes from inactive.png to active.png
        if (this.previous) {
    // this is to close the previously opened infowindow.
          this.previous.close();
        }
        this.previous = infowindow;
      }
Rosenbaum answered 16/10, 2018 at 13:30 Comment(0)
E
1

Instead of using a boolean, you could have a variable on your component storing the selected index (or element id if you have it) and checking if index === this.selectedIndex you can set icon active or disabled.

url: index === this.selectedIndex ? './assets/images/marker_active.png' : './assets/images/marker_inactive.png',
Excaudate answered 16/10, 2018 at 15:45 Comment(2)
Could you please check this? #52868344Rosenbaum
#53006083 . Pls chk this.Rosenbaum

© 2022 - 2024 — McMap. All rights reserved.