我在Gesoerver2.12WFS中使用OpenLayers 4。我想在每1秒添加新功能到向量源并显示在地图上。
现在,我的问题是,没有vectorSource.clear(),我就不能添加新特性。
如何在不清除“旧”功能的情况下添加新功能?
我的代码:
var url = "http://localhost:8080/geoserver/wfs?service=WFS&version=1.1.0&request=GetFeature&typename=wfs_geom&propertyName=geometry,id2&sortBy=id2+D&maxFeatures=1&srsname=EPSG:3857&outputFormat=application/json"
var vectorSource = new ol.source.Vector({
projection: 'EPSG:3857',
format: new ol.format.GeoJSON(),
url: url
});
var fetchData = function() {
jQuery.ajax(url, {
dataType: 'json',
success: function(data, textStatus, jqXHR) {
//vectorSource.clear();
console.log(data);
vectorSource.addFeatures(data);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
});
updateTimer = setTimeout(function() {
fetchData();
}, 3000);
};
fetchData();发布于 2018-04-20 08:05:21
你差点就拿到了。vectorSource.addFeature(feature);是正确的方法。但是,它只接受ol.Feature对象,而不接受原始数据。format选项的VectorSource只适用于通过url加载,它不用于addFeatures。
但是,将数据转换为特性很容易:
var feature = new ol.format.GeoJSON()({ featureProjection: 'EPSG:3857' }).readFeature(data));
vectorSource.addFeatures(feature);或者阅读多个特性:
var features = new ol.format.GeoJSON()({ featureProjection: 'EPSG:3857' }).readFeatures(data));
vectorSource.addFeaturess(features);https://stackoverflow.com/questions/49925035
复制相似问题