我想從地圖上刪除特定的標記,對於這一點,我已經寫了下面的JavaScript /谷歌地圖 - 無法刪除標記
<button (click)="removeDriver(id)"></button>
removeDriver(userId) {
//remove from the array a particular user
this.places = this.places.filter((user) => {
return user.userId !== userId;
});
let currentDriverLocation;
//the array elements are updated now, and the markers should be plotted again
this.places.forEach((driver) => {
currentDriverLocation = new google.maps.LatLng(driver.currentLocation.lat, driver.currentLocation.long);
this.marker = new google.maps.Marker({ position: currentDriverLocation, map: this.map });
})
}
數組與新對象更新;但是,沒有標記被刪除。
this.places排列如下所示:
[{"userId":"khfdg999","currentLocation":{"lat":59.02922, "lng":-12.3932}},
{"userId":"a85jfdo","currentLocation":{"lat":59.02922, "lng":-12.3932}}]
'數組用新對象更新;但是,沒有標記被刪除。「那是因爲你沒有刪除它們。在過濾數組時,您需要將其刪除。你需要調用'setMap(null)' – Adam
請看看這篇文章https://stackoverflow.com/questions/16482949/google-map-api-removing-markers它有一個答案,其中描述瞭如何刪除標記 –
您能否詳細闡述一下?我試圖在filter()之前調用'this.marker.setMap(null)',但沒有任何變化... –