1

1 new feature时添加style属性,new VectorLayer使用style属性(少见)

const feature = new Feature({
    id:1,
    geometry: new Point(fromLonLat([103.22,34.23])), // 注意自己的经纬度坐标系
    style:new Style({
     stroke: new Stroke({
         color: "#07B9EE",
         lineDash: [7, 10],
         width: 2
         })
    })
});

new VectorLayer({
 style: function(feature: any) {
    return feature.get("style");
 },
 source: new VectorSource({ features: [feature] })
 });

2 new feature后,调用setStyle方法,new Layer时调用feature的getStyle()方法

const feature = new Feature({
    id:1,
    geometry: new Point(fromLonLat([103.22,34.23])), // 注意自己的经纬度坐标系
});
feature.setStyle(() => {
    return new Style({
     stroke: new Stroke({
         color: "#07B9EE",
         lineDash: [7, 10],
         width: 2
         })
    })
})
new VectorLayer({
 style: function(feature: any) {
    return feature.getStyle(); // 注意与方法1的区别
 },
 source: new VectorSource({ features: [feature] })
 });

3 new VectorLayer 为layer中的所有feature时添加样式

new VectorLayer({
 style:new Style({
     stroke: new Stroke({
         color: "#07B9EE",
         lineDash: [7, 10],
         width: 2
     })
 }),
 source: new VectorSource({ features: [feature] })
 });

纤细的一帆风顺
40 声望2 粉丝

引用和评论

0 条评论