<div #gmap class="map"></div>
for (let marker of this.markersDisplay) {
let markerColor = (marker.MarkerType == MarkerType.Ok) ? "green" : (marker.MarkerType == MarkerType.Warning) ? "yellow" : "red";
let markerClick = new google.maps.Marker(
{
position: new google.maps.LatLng(marker.Latitude, marker.Longitude),
map: this.map,
title: marker.Title,
visible: marker.Visible,
clickable: marker.Clickable,
icon: 'http://maps.google.com/mapfiles/ms/icons/' + markerColor + '-dot.png',
});
markerClick.addListener('click', () => { this.MarkerClick(marker); });
}
}
我需要过滤地图上的标记。在添加新标记之前,我要清除地图上的所有标记。该怎么做?
答案 0 :(得分:2)
如何一次删除所有内容?
不能。将每个Marker对象推送到一个数组:
// Create empty markers array
var markers = [];
for (let marker of this.markersDisplay) {
let markerColor = (marker.MarkerType == MarkerType.Ok) ? "green" : (marker.MarkerType == MarkerType.Warning) ? "yellow" : "red";
let markerClick = new google.maps.Marker({
position: new google.maps.LatLng(marker.Latitude, marker.Longitude),
map: this.map,
title: marker.Title,
visible: marker.Visible,
clickable: marker.Clickable,
icon: 'http://maps.google.com/mapfiles/ms/icons/' + markerColor + '-dot.png',
});
markerClick.addListener('click', () => {
this.MarkerClick(marker);
});
// Push marker to markers array
markers.push(markerClick);
}
稍后,当您要删除所有标记时,遍历数组并在每个标记上调用setMap(null)
:
for (var i=0; i<markers.length; i++) {
markers[i].setMap(null);
}
答案 1 :(得分:0)
您可以尝试在标记上运行foreach,然后设置:
marker.setMap(null);
marker = null;