无法应用 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);
                };

EDIT 当我尝试使用 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);
                };

好久没玩OL3了,正在看文档

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

Features can be styled individually with setStyle; otherwise they use the style of their vector layer or feature overlay.

因此,在创建 "class instance" 时,可能无法将样式作为属性传递。

如果您尝试先创建功能,然后再做会怎么样

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% 有效。

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 就不会重新创建样式。