简体   繁体   中英

Access default OpenLayers 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?

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM