高德地图自定义marker并定时更新marker,先清除原先marker不起作用

问题遇到的现象和发生背景 需要把自定义图标定时刷新,就是定时替换成后端传过来的数据,用this.map.clear()方法清除再重新渲染图标,但是不起作用
用代码块功能插入代码,请勿粘贴截图

-

mounted() {
  this.getData() // 获取数据
  this.map.clear()
}
 // initMarker(AMap) 再初始地图中调用
 initMarker(AMap) {
      this.list.forEach((item) => {
        var IconImg = new AMap.Icon({
          // 图标尺寸
          size: new AMap.Size(80, 40),
          // 图标的取图地址
          // image: require('../assets/icon_btk41kc4q9m/Bus.png'),
          image: item.icon,
          // 图标所用图片大小
          imageSize: new AMap.Size(40, 20),
          // imageSize: new AMap.Size(80, 40),
          // 图标取图偏移量
          // imageOffset: new AMap.Pixel(-9, -3)
        })
        this.marker = new AMap.Marker({
          map: this.map,
          // position: this.pereptionInfoList.map(item => [item.longitude,item.latitude])[0],
          // position: [119.980915962, 30.520096745],
          position: item.position,
          // position: new AMap.LngLat(119.980915962, 30.520096745),
          icon: IconImg,
          offset: new AMap.Pixel(-35, -15), //调整图片偏移
          // offset: new AMap.Pixel(-56, -30), //调整图片偏移
          // autoRotation: true, //自动旋转
          // angle: -90 //图片旋转角度
        })
        //多点文本标记 longitude:经度   30.621637
        this.marker.setLabel({
          offset: new AMap.Pixel(10, 10), //设置文本标注偏移量
          // content: "
我是 marker 的 label 标签
"
, //设置文本标注内容 content: item.id, //设置文本标注内容 direction: 'top', //设置文本标注方位 }) }) },

你这个list里应该要创建很多marker吧,但是this.marker只记录了最后创建的一个,所有用map.remove(this.marker)清除不掉所有的,如果只想保留地图的话用map.clearMap()

你调用的时机 可能不对 。你需要 先 等地图 和 maker出来 再调用清除才行吧 。 有可能你先执行了清除 。再执行的 绘制