mapbox中removeSource出错

添加了一个清除图层的按钮

    const clearMarker = () => {
      const map: any = props.map;
      map.removeLayer('markerLyr');
      map.removeSource("marker");
    }

但是点击按钮,总是报错,先记录一下,显示The layer 'markerLyr' does not exist in the map's style and cannot be removed.There is no source with this ID
注释掉函数内容,console.log某个值,发现函数执行了两遍,暂不知原因。

在vue中将@click改为@click.once解决,这样有一个弊端,就是事件只执行一次就不再执行了,

另一种方式,做一个定时器

      setTimeout(() => {
        map.removeLayer('markerLyr');
        map.removeSource("markerSrc");
      }, 500);

posted @ 2021-11-22 15:15  青川薄  阅读(1029)  评论(0)    收藏  举报