【问题标题】:Access default OpenLayers style访问默认的 OpenLayers 样式
【发布时间】:2017-11-21 18:30:22
【问题描述】:

我在图层上设置了ol.StyleFunction

function style(feature: ol.Feature, resolution: number): ol.style.Style {
  return ol.style.Style({
    // stuff from the feature properties
  });
}

并非所有功能都包含自己的样式信息。 在这种情况下,我想回退到默认样式。

function style(feature: ol.Feature, resolution: number): ol.style.Style {
  if (!hasOwnStyle(feature)) {
    // defaultStyle is private :(
    return ol.style.Style.defaultStyle();
  }
  return ol.style.Style({
    // stuff from the feature properties
  });
}

有没有办法访问默认样式?

【问题讨论】:

  • 你试过return feature.getStyle();??????如果未提供,则应分配。
  • @pavlos 真的,好主意!

标签: javascript openlayers-3


【解决方案1】:

您可以将默认样式设置回来

import style from 'ol/style';

var fill = new ol.style.Fill({
   color: 'rgba(255,255,255,0.4)'
 });
 var stroke = new ol.style.Stroke({
   color: '#3399CC',
   width: 1.25
 });
 var styles = [
   new ol.style.Style({
    image: new ol.style.Circle({
       fill: fill,
       stroke: stroke,
       radius: 5
     }),
     fill: fill,
     stroke: stroke
   })
 ];

documentation所示。

【讨论】:

    【解决方案2】:

    将返回默认样式的样式函数分配给新创建的矢量图层。运行函数即可获取样式数组

    var defaultStyles = new ol.layer.Vector().getStyleFunction()();
    

    编辑风格是需要几何特征的功能

    var defaultEditingStyleFunction = new ol.interaction.Select().getOverlay().getStyleFunction();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-21
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多