无法以编程方式向图层添加要素

Unable to add feature programmatically to the layer

我的地图附加了一个点击事件。在这个点击事件中,我触发了一个应该向地图添加功能的功能,但现在什么也没有发生。我这样试过:

function boo (map, layer){
    var source = layer.getSource();
    var thing = new ol.geom.Polygon( [[
       ol.proj.transform([-16,-22], 'EPSG:4326', 'EPSG:3857'),
       ol.proj.transform([-44,-55], 'EPSG:4326', 'EPSG:3857'),
       ol.proj.transform([-88,75], 'EPSG:4326', 'EPSG:3857')
    ]]);
    var featurething = new ol.Feature({
        name: "Thing",
        geometry: thing,
        style: function() {
            console.log("Never see this text");
            return new ol.style.Style({
               fill: new ol.style.Fill({
                  color: "rgba(192,192,192,1)"
               }),
               stroke: new ol.style.Stroke({
                  color: "rgba(192,192,192,1)",
                  width: 10
               })
           })
        }
     });
     source.addFeature( featurething );
     // see no error messages, but still no feature is added to the map
}

It is a OL3 bug

没那么快。

函数的第一个参数应该是 click 事件。另一个错误:ol.Feature 构造函数中没有 style 参数。

创建后设置要素样式。所以:

featurething.setStyle(some_style_or_a_function);