1. 添加(将marker放在markers数组中为了方便删除)
let marker = new AMap.Marker({
map: that.aMap,
position: e.data.lnglat,
content: `<div style='color: #a2aee0; background: rgb(2, 19, 54); margin: -5px;border-width: 2px;border-color: #02e6ff;border-style: solid; border-radius: 4px;'>
<div class="close-btn" style="float: right;margin-top: 3px; margin-right: 5px;" onclick="clearMarker()">X</div>
<div style="height: 36px; line-height: 45px; padding: 0px 20px; white-space:nowrap;">位置:北京</div>
<div style="height: 36px; line-height: 20px; padding: 0px 20px; white-space:nowrap;">
联系人:袁磊
</div>
</div>
`
});
that.markers.push(marker);
2. 删除
if (that.markers && that.markers.length) {
that.markers.map((ele, index) => {
ele.setMap(null);
});
}
3. 点击marker中的事件(因把clearMarker事件挂载在window上就可以调用)
mounted() {
window.clearMarker = () => {
if (this.markers && this.markers.length) {
this.markers.map((ele, index) => {
ele.setMap(null);
});
}
};
},