如何删除地图框标记



我有一个名为draw((的函数。我在draw((中传递数组值,在地图上绘制我想要的标记,它运行良好!!

function  draw(j){

//for (let j =0; j< countvalue[0]; j++){
array[j].features.forEach(function (marker) {
// create a HTML element for each feature
var el = document.createElement('div');
el.className = 'marker';
var el = document.createElement('div');
el.className = 'marker';

el.style.backgroundImage =
'url(https:gin/images/pin.png)';
el.style.width = marker.properties.iconSize[0] + 'px';
el.style.height = marker.properties.iconSize[1] + 'px';
el.style.backgroundSize = '100%';
// make a marker for each feature and add it to the map

new mapboxgl.Marker(el)
.setLngLat(marker.geometry.coordinates)
.setPopup(
new mapboxgl.Popup({ offset: 25 }) // add popups
.setHTML('</h3><p>' + marker.properties.description + '</p>' )
)
.addTo(map);
});
}

我的问题:

我将如何调用draw((函数来删除标记?如果这不可能,请原谅。非常感谢您抽出时间

创建要添加到地图的标记列表,并在需要时传递一个值以清除所有现有标记。

let markersList = []
function  draw(j, clearAll){

if(clearAll){
if(markersList){
for (var i = markersList.length - 1; i >= 0; i--) {
markersList[i].remove();
}
}
}
//for (let j =0; j< countvalue[0]; j++){
array[j].features.forEach(function (marker) {
// create a HTML element for each feature
var el = document.createElement('div');
el.className = 'marker';
var el = document.createElement('div');
el.className = 'marker';

el.style.backgroundImage =
'url(https:gin/images/pin.png)';
el.style.width = marker.properties.iconSize[0] + 'px';
el.style.height = marker.properties.iconSize[1] + 'px';
el.style.backgroundSize = '100%';
// make a marker for each feature and add it to the map

const newMarker = new mapboxgl.Marker(el)
.setLngLat(marker.geometry.coordinates)
.setPopup(
new mapboxgl.Popup({ offset: 25 }) // add popups
.setHTML('</h3><p>' + marker.properties.description + '</p>' )
)
.addTo(map);
markersList.push(newMarker);
});
}

相关内容

  • 没有找到相关文章

最新更新