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 oftennull
, so it is better to usecreateDefaultStyles(feature, resolution)
. See my answer below. – Andrey Commented Oct 25, 2022 at 16:50
3 Answers
Reset to default 4A 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
版权声明:本文标题:javascript - Access default OpenLayers style - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744056754a2583394.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论