添加了一個清除圖層的按鈕
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);