Javascript 删除矢量层
我正在尝试删除矢量源层以重新绘制标记。问题是,当我每3秒执行一次setInterval函数时,新的标记与以前的标记重叠。不会删除以前的标记 我正在尝试使用Javascript 删除矢量层,javascript,openlayers,openlayers-3,Javascript,Openlayers,Openlayers 3,我正在尝试删除矢量源层以重新绘制标记。问题是,当我每3秒执行一次setInterval函数时,新的标记与以前的标记重叠。不会删除以前的标记 我正在尝试使用 map.getLayers().item(1.getSource().clear(); map.getLayers().item(1.getSource().getSource().clear(); 但它不起作用 因此: Mi代码为: var vectorSource = new ol.source.Vector({ features:
map.getLayers().item(1.getSource().clear(); map.getLayers().item(1.getSource().getSource().clear(); 但它不起作用 因此: Mi代码为:
var vectorSource = new ol.source.Vector({
features: iconFeatures //add an array of features
});
var clusterSource = new ol.source.Cluster({
source: vectorSource,
distance: 40
});
var vectorLayer = new ol.layer.Vector({
source: clusterSource,
style: clusterStyle
});
// Maps
var map = new ol.Map({
controls: ol.control.defaults({
attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
collapsible: false
})
}),
target: 'map', // The DOM element that will contains the map
renderer: 'canvas', // Force the renderer to be used
layers: [
// Add a new Tile layer getting tiles from OpenStreetMap source
new ol.layer.Tile({
source: new ol.source.OSM()
//source: new ol.source.OSM({
//crossOrigin: null,
//url: 'http://34.240.39.198/osm_tiles/{z}/{x}/{y}.png',
//}),
}),
vectorLayer,
],
// Create a view centered on the specified location and zoom level
view: new ol.View({
center: ol.proj.transform([-3.7467975, 40.3705281], 'EPSG:4326', 'EPSG:3857'),
zoom: 3,
maxZoom: 15,
minZoom:2,
//extent: [226838, 5084100, 255700, 5055200],
}),
/*interactions: ol.interaction.defaults({
dragPan: false
}),*/
});
重画的功能是:
function get_traces(){
var token = $('meta[name="csrf-token"]').attr('content');
$.ajax({
type: "post",
url: "device_mapA",
typeData:'JSON',
data: {
'_method': 'POST',
'_token': token,
}, success: function (showdevice) {
removeAllMarkers();
iconFeatures = [];
showdevice[0].forEach(function(index) {
changeMarker(showdevice[0]); //this function redraw iconFeatures array correctly.
});
});
// console.log(iconFeatures);
var vectorSource = new ol.source.Vector({
features: iconFeatures //add an array of features
});
var clusterSource = new ol.source.Cluster({
source: vectorSource,
distance: 40
});
var vectorLayer = new ol.layer.Vector({
// source : vectorSource,
source: clusterSource,
style: clusterStyle
});
map.getLayers().item(1).getSource().clear();
map.getLayers().item(1).getSource().getSource().clear();
map.addLayer(vectorLayer);
map.getLayers().item(1).getSource().clear();
//console.log(map.getLayers().item(1).getSource()); It not working neither.
}
它实际上并没有中止这7次迭代,只是跳过了那些数组项 在forEach循环中,有一个映射层的引用数组。如果您获取该数组的一个元素(引用是“层”)并按原样从映射中删除它,则会删除该引用,因此它不再位于映射中或数组中,并且意外地在该索引上存在另一个引用 因此,如果您有数组: 0:layer0,名称为“layer0” 1:第1层,名称为“第1层” 2:第2层 在这场战争之后,还将继续存在 0:layer1,名称为“layer1” 1:第2层 因为在删除layer0之后,索引0上有layer1,然后forEach移动(到索引1),在那里它已经找到了没有名称的层
要解决这个问题,只需使用函数getArray()和slice()(复制引用数组),类似这样:谢谢您的回答。你有什么例子吗?您的答案以“类似这样的内容”结尾:“无论如何,我使用了以下代码:for(I=0;I==10;I++){map.getLayers().item(I).getSource().clear();map.getLayers().item(I).getSource().getSource().clear();}。而且不工作。