[英]Angular 6 Remove all markers from google map
<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); });
}
}
我需要過濾地圖上的標記。 在添加新標記之前,我要清除地圖上的所有標記。 怎么做?
如何一次全部刪除?
你不能 將每個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);
}
稍后,當您要全部刪除它們時,遍歷數組並在每個Marker上調用setMap(null)
:
for (var i=0; i<markers.length; i++) {
markers[i].setMap(null);
}
您可以嘗試在標記上運行foreach,然后設置:
marker.setMap(null);
marker = null;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.