首页 > 解决方案 > 为什么我不能运行这个方法(openlayers)(ol.layer.Vector)

问题描述

   
    var vectorSource = new ol.source.Vector({
        features: features
    });
    
    var vectorLayer = new ol.layer.Vector({
        source: vectorSource,
        
        style: function (feature) {
            feature.setStyle(typeitems(feature));
            console.log("apple");
        }
    });

这是我的代码。为什么不能运行这一行 console.log("apple"); 在 ol.layer.Vector 中?

标签: openlayers

解决方案


style属性将应用于ol.Style | ol.Style[] | ol.StyleFunction所有没有自己风格的特征。

如果您发布完整的示例,我们可能会为您提供更好的帮助。但是,我将假设样式函数签名类似于function typeitems(feature: ol.Feature): ol.Style. 在这种情况下,您可以像这样重写您的代码:

var vectorLayer = new ol.layer.Vector({
    source: vectorSource,

    style: function (feature) {
        return typeitems(feature);
    }
});

[...]

function typeitems(feature: ol.Feature): ol.Style {
    return new ol.Style({
        stroke: new ol.Style.Stroke({ color: 'red', width: 5})
    });
}

在这里,typeitems()有点毫无意义,因为它总是会创建相同的红色笔触样式,但你明白我的意思是如何解决你的问题。如果您需要更具体的帮助,请发布比这种最小变量声明更多的相关代码。

祝你好运!


推荐阅读