OpenLayers 4 Vector - 添加新功能,但不清除其他功能

OpenLayers 4 Vector - append new features, but without clearing others

我将 OpenLayers 4 与 Gesoerver 2.12 WFS 结合使用。我想每 1 秒添加一个新功能到 Vector Source 并显示在地图上。

现在,我的问题是没有 vectorSource.clear() 就无法添加新功能。

如何在不清除 "old" 功能的情况下添加新功能?

我的代码:

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();

你快明白了。 vectorSource.addFeature(feature); 是正确的方法。 但是,它只接受 ol.Feature 个对象,不接受原始数据。 VectorSource 的 format 选项仅适用于通过 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);