Openlayers 3 OpenLayers 3-获取渲染功能的应用样式
是否可以获取所选特征的应用样式?我的图层使用样式函数而不是ol.style.style。调用myFeature.getStyle()时,它返回函数 根据这里和其他地方的回答,用我迄今为止尝试过的一些东西更新我的问题 最后,我为每个层创建了一个选择样式函数 这不起作用,不返回任何样式:Openlayers 3 OpenLayers 3-获取渲染功能的应用样式,openlayers-3,Openlayers 3,是否可以获取所选特征的应用样式?我的图层使用样式函数而不是ol.style.style。调用myFeature.getStyle()时,它返回函数 根据这里和其他地方的回答,用我迄今为止尝试过的一些东西更新我的问题 最后,我为每个层创建了一个选择样式函数 这不起作用,不返回任何样式: selectInteraction = new ol.interaction.Select( { style: function( feature, resolution ) { if (
selectInteraction = new ol.interaction.Select( {
style: function( feature, resolution ) {
if ( feature.selectStyle ) {
return feature.selectStyle;
}
},
layers: mylayers
} );
此外,通过上述操作,我得到以下错误:
Uncaught TypeError: style.getImage is not a function
ol.renderer.vector.renderFeature @ol.js:38455
ol.renderer.canvas.VectorLayer.renderFeature @ol.js:44202
renderFeature @ol.js:44148
ol.renderer.canvas.VectorLayer.prepareFrame @ol.js:44164
ol.renderer.canvas.Map.renderFrame @ol.js:45268
ol.Map.renderFrame_ @ol.js:52034
(anonymous function) @ol.js:50898
这确实有效,使用“我的选择”样式渲染特征。但是,我不想管理重新设置样式,因此它确实需要在selectInteraction中实现:
evt.selected.forEach( function( evt ) {
evt.setStyle( evt.selectStyle );
}, this );
我认为您必须使用
myFeature.getStyleFunction()
而不是myFeature.getStyle()
祝您好运。您应该使用该功能调用函数(如果您的样式函数依赖于它,还可以选择使用分辨率)。比如:myFeature.getStyle().call(this,feature);或myFeature.getStyle().call(this,feature,resolution) 我的解决方法是分别创建与功能不同的样式,然后使用setStyle()应用它。我还将样式保存在功能中的其他位置,以便以后检索
let myStyle = new ol.style.Style({
image: new ol.style.RegularShape({
fill: new ol.style.Fill({
color: 'cyan'
}),
stroke: new ol.style.Stroke({
color: 'black',
width: 1
}),
points: 4,
radius: 9,
angle: Math.PI / 4
}),
});
myFeature.setStyle(myStyle);
myFeature.set('myStyle', myStyle); // save it for later use
在“选择”过程中,只需使用:
feature.get('myStyle');
检索应用于该功能的样式。我尝试了此操作,但myFeature.getStyle()返回null。特性的styleFunction_uu属性在设置样式时是如何未定义的?这就是解决方法!谢谢分享!