admin管理员组

文章数量:1356808

I have a ol.StyleFunction set on a layer.

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

Not all of the features contain their own styling information. In this case, i'd like to fall back to the default style.

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
  });
}

Is there a way to access the default style?

I have a ol.StyleFunction set on a layer.

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

Not all of the features contain their own styling information. In this case, i'd like to fall back to the default style.

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
  });
}

Is there a way to access the default style?

Share Improve this question asked Nov 21, 2017 at 18:30 Ilia CholyIlia Choly 18.6k14 gold badges94 silver badges164 bronze badges 3
  • 1 Did you try return feature.getStyle();?????? It should be assigned if not provided. – pavlos Commented Nov 23, 2017 at 16:12
  • @pavlos true, good idea! – Ilia Choly Commented Nov 24, 2017 at 0:11
  • feature.getStyle() is often null, so it is better to use createDefaultStyles(feature, resolution). See my answer below. – Andrey Commented Oct 25, 2022 at 16:50
Add a ment  | 

3 Answers 3

Reset to default 4

A style function which returns the default style is assigned to newly created vector layers. You can get the style array by running the function

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

The editing style is a function which requires a feature with geometry

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

You can set the default style back

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
   })
 ];

As showed in the documentation.

import { FeatureLike } from 'ol/Feature/'
import Style from 'ol/style/Style'
import { createDefaultStyle } from 'ol/style/Style'

function getStyles(feature: FeatureLike, resolution: number): Style[] {
  // return default styles (array)
  return createDefaultStyle(feature, resolution)
}

// or

function getStyle(feature: FeatureLike, resolution: number): Style {
  // return default style
  return createDefaultStyle(feature, resolution)[0]
}

本文标签: javascriptAccess default OpenLayers styleStack Overflow