首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenLayers 4向量-附加新特性,但不清除其他特性

OpenLayers 4向量-附加新特性,但不清除其他特性
EN

Stack Overflow用户
提问于 2018-04-19 15:37:53
回答 1查看 1.2K关注 0票数 1

我在Gesoerver2.12WFS中使用OpenLayers 4。我想在每1秒添加新功能到向量源并显示在地图上。

现在,我的问题是,没有vectorSource.clear(),我就不能添加新特性。

如何在不清除“旧”功能的情况下添加新功能?

我的代码:

代码语言:javascript
复制
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();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-20 08:05:21

你差点就拿到了。vectorSource.addFeature(feature);是正确的方法。但是,它只接受ol.Feature对象,而不接受原始数据。format选项的VectorSource只适用于通过url加载,它不用于addFeatures

但是,将数据转换为特性很容易:

代码语言:javascript
复制
var feature = new ol.format.GeoJSON()({ featureProjection: 'EPSG:3857' }).readFeature(data));
vectorSource.addFeatures(feature);

或者阅读多个特性:

代码语言:javascript
复制
var features = new ol.format.GeoJSON()({ featureProjection: 'EPSG:3857' }).readFeatures(data));
vectorSource.addFeaturess(features);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49925035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档