高德地图添加marker、删除marker和点击marker里面的heml按钮

文章讲述了如何在地图上添加自定义标记,并通过JavaScript实现添加到markers数组中,以及在满足条件时删除所有标记。同时,还介绍了如何处理点击marker触发的事件,通过挂载在window上以便全局访问.

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
        });
      }
    };
  },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值