【发布时间】:2017-10-29 20:52:54
【问题描述】:
我正在尝试从地图中删除特定标记,为此,我写了以下内容
<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}}]
【问题讨论】:
-
The array is updated with the new objects; however, no markers get deleted.那是因为您没有删除它们。过滤数组时需要删除它们。您需要致电setMap(null) -
请看这篇帖子stackoverflow.com/questions/16482949/… 它有一个答案,其中描述了如何删除标记span>
-
您能详细说明一下吗?我曾尝试在
filter()之前致电this.marker.setMap(null),但没有任何改变... -
无论出于何种原因,除非我存储标记的数组可以从全局/模块范围访问,否则它对我不起作用。我在使用 Vue 并将标记存储在我的组件的实例属性上不起作用。
标签: javascript google-maps google-maps-api-3