简体   繁体   English

不透明性不适用于openlayers 5中的Fill for KML层

[英]Opacity is not applied for Fill for KML layer in openlayers 5

I am currently upgrading our openlayers from 2.x to 5.3. 我目前正在将openlayers从2.x升级到5.3。 We have some KML files on the server which should be displayed as individual layers on the map. 服务器上有一些KML文件,应将其显示为地图上的各个图层。 This is working perfectly fine after the upgrade except that the fill of the polygons of the KML do not apply the opacity provided in the color alpha [r,g,b,alpha]. 升级后,此操作非常正常,除了KML多边形的填充不应用颜色alpha [r,g,b,alpha]中提供的不透明度。

I know this issue has been discussed frequently but the suggested solution always included to add the opacity in the alpha of the color, which is not working for me. 我知道这个问题经常被讨论,但是建议的解决方案始终包括在颜色的Alpha中添加不透明度,这对我不起作用。 I tried to use both, a color array and defining the color via rgba text. 我试图同时使用颜色数组和通过rgba文本定义颜色。 But the opacity of the KML content fill is always one. 但是KML内容填充的不透明度始终是一种。

However, the opacity is working when I add the opacity field to the layer object. 但是,当我将不透明度字段添加到图层对象时,不透明度正在工作。 But I only need the fill to have an opacity and not the stroke. 但我只需要使填充具有不透明度即可,而无需笔触。 As this might be of interest, I have integrated Google layers in my project by applying the OL3GM project. 因为这可能很有趣,所以我通过应用OL3GM项目将Google图层集成到了我的项目中。 Thus, the ol.js file is integrated by this ol3gm project. 因此,此ol3gm项目集成了ol.js文件。

//pConfig contains all the relevant information
var lKmlLayers = [];
for(var i = 1; i < pConfig['kmlFiles'].length;++i) {
    var lLayerName = getBaseName(pConfig['kmlFiles'][i][1]);
    var lDropBoxPathParamValue = '?dropBoxPath=' + pConfig['kmlFiles'][i][0];
    var lContextParamValue = '&context=' + pConfig['context'];
    var lProjectIdParamValue = '&projectId=' + pConfig['projectId'];
    var lKmlFileNameParamValue = '&kmlFileName=' + pConfig['kmlFiles'][i][1];
    //here I define the fill color and set the opacity value
    var fillColor = ol.color.asArray(pConfig['kmlFiles'][i][2]);
    fillColor = fillColor.slice();
    fillColor[3] = 0.3;
    var strokeColor = ol.color.asArray(pConfig['kmlFiles'][i][2]);
    strokeColor = strokeColor.slice();
    strokeColor[3] =1;
    lKmlLayers[i-1] = new ol.layer.Vector({
        title:lLayerName,
        source: new ol.source.Vector({
        url: pConfig['context'] + '/filedata' + lDropBoxPathParamValue + lContextParamValue + lProjectIdParamValue + lKmlFileNameParamValue,
        format: new ol.format.KML()
        }),
        style: new ol.style.Style({
            fill: new ol.style.Fill({
                    color: fillColor
            }),
            stroke: new ol.style.Stroke({
                    color: strokeColor,
                    width: 1
            })
        }),
        //opacity: 0.3//if I add this, the opacity is applied for the complete layer
    });
}

The opacity of the fill is not applied. 不应用填充的不透明度。 The KML layers fill is always displayed with an opacity of 1 although I set it to 0.3. 尽管我将其设置为0.3,但KML图层填充始终显示为1。 Can somebody tell me why and tell me how to fix this? 有人可以告诉我原因并告诉我如何解决此问题吗? Thank you for your support. 谢谢您的支持。

In this case specifying 在这种情况下,

format: new ol.format.KML({ extractStyles: false })

was sufficient to prevent styles being applied directly to the features and allow the layer style to take effect. 足以防止将样式直接应用于要素并允许图层样式生效。 In some cases that might not work as OpenLayers can apply a default style and you would need to loop through the features and remove the styles. 在某些情况下,这可能无法正常工作,因为OpenLayers可以应用默认样式,因此您需要循环浏览功能并删除样式。 See Heatmap in Openlayers using an XML (KML formatted) string, styling is incorrect 请参阅使用XML(KML格式)字符串的Openlayers中的Heatmap,样式不正确

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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