访问默认的OpenLayers样式

问题描述 投票:2回答:2

我在一层上设置了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
  });
}

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

javascript openlayers-3
2个回答
2
投票

您可以返回默认样式

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所示。


1
投票

返回默认样式的样式函数将分配给新创建的矢量图层。您可以通过运行该函数来获取样式数组

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

编辑样式是一个需要具有几何特征的功能

var defaultEditingStyleFunction = new ol.interaction.Select().getOverlay().getStyleFunction();
© www.soinside.com 2019 - 2024. All rights reserved.