openlayers - 从 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。我只希望在导入数据时应用一次样式。
任何帮助将非常感激!
每个功能
解决方案
你可以使用
layer.getSource().on('addfeature', function(feature) {
feature.setStyle(customStyleFunction(feature));
});
推荐阅读
- javascript - 为什么在连接到 mailchimp API 3.0 时出现 401 错误
- python - 如何将字典列表添加到另一个字典?
- shopify - 使用 Shopify Liquid 的数千个分离过滤器
- android - 如何将文本和链接从 android 应用程序共享到 Messenger?
- c# - 在 Unity3d 中无法按名称找到对象 - 导致 NRE
- datagrid - 如何从清晰数据网格中隐藏 selectAll 复选框?
- kubernetes - Pod 安全策略未按预期工作
- python - python opencv准确获取系统时间的相机帧时间戳
- android - Android 中不使用希腊语
- java - CheckStyle 不会在接口方法上标记缺少的 JavaDoc