Pat*_*cow 30 javascript maps google-maps leaflet
我有以下代码:
map: function (events) {
var arrayOfLatLngs = [];
var _this = this;
// setup a marker group
var markers = L.markerClusterGroup();
events.forEach(function (event) {
// setup the bounds
arrayOfLatLngs.push(event.location);
// create the marker
var marker = L.marker([event.location.lat, event.location.lng]);
marker.bindPopup(View(event));
// add marker
markers.addLayer(marker);
});
// add the group to the map
// for more see https://github.com/Leaflet/Leaflet.markercluster
this.map.addLayer(markers);
var bounds = new L.LatLngBounds(arrayOfLatLngs);
this.map.fitBounds(bounds);
this.map.invalidateSize();
}
Run Code Online (Sandbox Code Playgroud)
我最初调用此函数,它将添加所有events
标记和群集的地图.
在某些其他事件中传递的泡沫点,地图将放大到新事件,但旧的事件仍然在地图上.
我尝试了this.map.removeLayer(markers);
一些其他的东西,但我不能让旧的标记消失
有任何想法吗?
iH8*_*iH8 41
如果要删除组中的所有当前图层(标记),可以使用clearLayers
方法L.markerClusterGroup()
.您的参考被调用,markers
因此您需要致电:
markers.clearLayers();
Run Code Online (Sandbox Code Playgroud)
小智 16
你丢失了标记参考,因为它是用var设置的.请尝试将引用保存为"this".
mapMarkers: [],
map: function (events) {
[...]
events.forEach(function (event) {
[...]
// create the marker
var marker = L.marker([event.location.lat, event.location.lng]);
[...]
// Add marker to this.mapMarker for future reference
this.mapMarkers.push(marker);
});
[...]
}
Run Code Online (Sandbox Code Playgroud)
然后当你需要删除标记运行时:
for(var i = 0; i < this.mapMarkers.length; i++){
this.map.removeLayer(this.mapMarkers[i]);
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以将群集保存为"this",而不是将每个引用保存到每个标记.
归档时间: |
|
查看次数: |
61721 次 |
最近记录: |