let marker = [];
//循環添加地圖中的點標記
res.result.equipments.forEach((item, index) => {
var temp = new AMap.Marker({
position: new AMap.LngLat(item.areaLongitude, item.areaLatitude),
offset: new AMap.Pixel(-10, -10),
icon: new AMap.Icon({
size: new AMap.Size(40, 40), // 圖標尺寸
image: _this.picHttp + item.image,//絕對路徑
imageSize: new AMap.Size(40, 40)
}),
extData: {//自定義屬性
id: item.id,
state: item.state
},
map: _this.map,
clickable: true
});
marker.push(temp);
AMap.event.addListener(temp, "click", function(e) { //每個點標記的點擊事件
_this.showType = this.getExtData().state;
_this.getWeatherStationById(this.getExtData().id);
//獲取點標記的自定義數據 this.getExtData()
if (this.getExtData().state == 2) {
_this.$router.push({
path: "/Camera",
query: { id: this.getExtData().id }
});
}
if (_this.clickedMarker === e.target) return; // 點擊的是同一個Marker
if (_this.clickedMarker) {
// 把上次點擊的Marker的樣式恢復至原來模樣
_this.clickedMarker.setOffset(new AMap.Pixel(-10, -10));
_this.clickedMarker.setIcon(
new AMap.Icon({
image: _this.images,
size: new AMap.Size(40, 40), //圖標大小
imageSize: new AMap.Size(40, 40)
})
);
}
this.setIcon(
new AMap.Icon({
image: _this.picHttp + item.image,
size: new AMap.Size(50, 50), //圖標大小
imageSize: new AMap.Size(50, 50)
})
);
e.target.setOffset(new AMap.Pixel(-18, -20));
_this.clickedMarker = e.target; //data中存儲上一次點擊事件
_this.images = e.target.getIcon().B.image;//data中存儲上一次點擊事件中的圖片
});
});