繁体   English   中英

无法获取要应用的Openlayers 3样式

[英]Can't get Openlayers 3 style to be applied

我正在敲墙,想知道为什么不采用这种风格。 点以默认样式渲染。

                if ((Math.abs(prevCoord[0] - currCoord[0]) < 500) && (Math.abs(prevCoord[1] - currCoord[1]) < 500)) {
                    console.log("tortuous");
                    var tortySource = new ol.source.Vector(); // create an empty source instance

                    var tortyPoint = new ol.geom.Point(currCoord);

                    var tortyFeature = new ol.Feature({ // create a feature with the point geometry
                        geometry: tortyPoint,
                        style: new ol.style.Style({
                            fill: new ol.style.Fill({
                                color: 'rgba(255,0,0,0.5)'
                            })
                        })
                    });

                    tortySource.addFeature(tortyFeature); // add the feature to the source

                    var tortyLayer = new ol.layer.Vector({ // create a layer with that source
                        source: tortySource
                    });

                    map.addLayer(tortyLayer);
                };

编辑当我尝试使用setStyle时,我是这样做的。 我所有的观点都消失了。

                if ((Math.abs(prevCoord[0] - currCoord[0]) < 500) && (Math.abs(prevCoord[1] - currCoord[1]) < 500)) {
                    console.log("tortuous");
                    var tortySource = new ol.source.Vector(); // create an empty source instance

                    var tortyPoint = new ol.geom.Point(currCoord);

                    var tortyFeature = new ol.Feature({ // create a feature with the point geometry
                        geometry: tortyPoint
                    });

                    tortyFeature.setStyle(
                        new ol.style.Style({
                            fill: new ol.style.Fill({
                                color: [255, 0, 0, 0.5]
                            })
                        })
                    );

                    tortySource.addFeature(tortyFeature); // add the feature to the source

                    var tortyLayer = new ol.layer.Vector({ // create a layer with that source
                        source: tortySource
                    });

                    map.addLayer(tortyLayer);
                };

ol.Feature没有样式属性。 您不能在构造函数上设置样式。 您应该使用ol.Feature#setStyle 所以:

feature.setStyle(
  new ol.style.Style({
    image: new ol.style.Circle({
        fill: new ol.style.Fill({ color: [255,0,0,1] }),
        stroke: new ol.style.Stroke({ color: [0,0,0,1] }),
        radius: 5
    })
  })
);

最好将样式存储在变量中,这样OL不会重新创建样式。

很长一段时间我不在OL3中,而是在看文档。

http://openlayers.org/en/v3.5.0/apidoc/ol.Feature.html

可以使用setStyle分别设置样式。 否则,他们将使用其矢量层或要素叠加层的样式。

因此,在创建“类实例”时将样式作为属性传递可能会行不通。

如果您尝试先创建功能然后执行

tortyFeature.setStyle({    
    fill: new ol.style.Fill({
        color: 'rgba(255,0,0,0.5)'
    })
}

另请注意,与文档一致的http://openlayers.org/en/v3.5.0/apidoc/ol.style.Style.html以及所有相关内容仍处于试验阶段。

或者尝试将样式添加到tortySource

这只是一种探索方式,我无法采取行动,这会100%起作用。

暂无
暂无

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

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