0
ラジオボタンを使用して地図に表示されている機能をフィルタリングしようとしています。私はボタンをクリックするとベクトルソースを変更することができました。しかし、マップは自動的に最後の元のベクトルソースと再び自動的にレンダリングされ、更新されたベクトルソースを上書きしたようです。変更されたベクトルソースはOpenLayers v4で維持されません
var styleCache = {};
var vectorSource = new ol.source.Vector({
url: 'sla.kml',
format: new ol.format.KML({
extractStyles: false
})
});
var clusters = new ol.layer.Vector({
source: new ol.source.Cluster({
distance: 40,
source: vectorSource
}),
style: function(feature) {
var size = feature.get('features').length;
var style = styleCache[size];
if (!style) {
style = new ol.style.Style({
image: new ol.style.Circle({
radius: 10,
stroke: new ol.style.Stroke({
color: '#fff'
}),
fill: new ol.style.Fill({
color: '#3399cc'
})
}),
text: new ol.style.Text({
text: size.toString(),
fill: new ol.style.Fill({
color: '#fff'
})
})
});
styleCache[size] = style;
}
return style;
}
});
var raster = new ol.layer.Tile({
source: new ol.source.OSM()
});
var mousePositionControl = new ol.control.MousePosition({
coordinateformat: ol.coordinate.createStringXY(4),
undefinedHTML: ' '
});
var scalelineControl = new ol.control.ScaleLine();
var zoomSlider = new ol.control.ZoomSlider();
var map = new ol.Map({
layers: [raster, clusters],
controls: ol.control.defaults().extend([scalelineControl, zoomSlider]),
renderer: 'canvas',
target: 'map',
view: new ol.View({
center: [-11102324.569458216, 4548521.327621765],
zoom:5
})
});
var count = 0;
var allFeatures;
function changeFeatures(status) {
if (count == 0) {
allFeatures = vectorSource.getFeatures();
}
count++;
// clear all features first, then add them back base on status
vectorSource.clear();
if (status === 'all') {
vectorSource.addFeatures(allFeatures);
} else {
var feature, name;
for (var i = allFeatures.length - 1; i >= 0; --i) {
feature = allFeatures[i];
if (feature.get('status') === status) {
vectorSource.addFeature(feature);
}
}
}
}
vectorSource.on('change', function(evt) {
var source = evt.target;
if (source.getState() === 'ready') {
var numfeatures = source.getFeatures().length;
console.log("feature count after change event: " + numfeatures);
}
});
コンソールからの出力を示しています変更イベントの後
- 特徴数:9 - 変更イベントの後>オリジナルの特徴
- 特徴数:0 - >クリアベクトルソース後
- 機能を変更イベント後のカウント:1 - >修飾されたフィーチャーを1つ追加した後
- 変更後のフィーチャー・カウント:9 - >元のベクター・ソースでマップを再度レンダリングするように見える(理由)
マップは元のベクターソースで再度レンダリングされるのはなぜですか?更新されたベクターソースでマップをレンダリングするには、何をする必要がありますか?