如何更改角度谷歌地图中先前单击的标记的图标 [英] How to change icon of previously clicked marker in angular google maps
问题描述
我设法使用以下代码更改了当前单击的标记的图标.我在页面上有多个标记.现在的问题是,如果我单击第二个标记,则先前单击的标记的图标应更改为其原始标记. (inactive.png)和当前单击的标记的图标应使用(active.png).如何实现?请帮忙.
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.
在下面的代码中,如果m.isClicked为true,则使用inactive.png,否则使用active.png.
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;
}
推荐答案
除了使用布尔值,您还可以在组件上使用一个变量来存储选定的索引(如果有,则为元素ID)并检查是否为index === this.selectedIndex
您可以将图标设置为活动或禁用.
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',
这篇关于如何更改角度谷歌地图中先前单击的标记的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!