从mongoDB geojson加载的样式功能

问题描述 投票:0回答:1

我有一个地图,其图层包含由用户制作的注释,该注释以geojson格式存储在mongoDB数据库中。

当页面加载时,我创建了一个矢量层来显示注释,例如:

features = new ol.format.GeoJSON().readFeatures(response);
var layer = new ol.layer.Vector({
      source: new ol.source.Vector({features: features})
});

这可以正常工作,并且我能够以默认样式显示所有功能。但是,每个功能确实具有与之关联的样式属性。如何浏览我创建的图层中的所有要素并将每个注释更改为正确的样式?

我曾尝试像这样制作图层时创建自定义样式功能:

var layer = new ol.layer.Vector({
      source: new ol.source.Vector({features: features})
      style: customStyleFunction
});

尽管这样做有效,但每次我平移,缩放等时都会运行customStyleFunction。我只希望在导入数据时将样式应用一次。

任何帮助将不胜感激!

每个功能

openlayers openlayers-3
1个回答
0
投票

您可以使用

layer.getSource().on('addfeature', function(feature) {
  feature.setStyle(customStyleFunction(feature));
});
© www.soinside.com 2019 - 2024. All rights reserved.