首页 > 解决方案 > 从 mongoDB geojson 加载的样式功能

问题描述

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

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

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。我只希望在导入数据时应用一次样式。

任何帮助将非常感激!

每个功能

标签: openlayersopenlayers-3

解决方案


你可以使用

layer.getSource().on('addfeature', function(feature) {
  feature.setStyle(customStyleFunction(feature));
});

推荐阅读