Jacobian
Jacobian

Reputation: 10812

Unable to add feature programmatically to the layer

I have a click event attached to my map. On this click event I trigger a function which is supposed to add a feature to the map, but now nothing happens. I tried it like so:

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
}

Upvotes: 1

Views: 916

Answers (1)

Jonatas Walker
Jonatas Walker

Reputation: 14150

It is a OL3 bug

Not so fast.

The first argument of your function should be the click event. Another error: there's no style parameter in ol.Feature constructor.

Set the feature style after its creation. So:

featurething.setStyle(some_style_or_a_function);

Upvotes: 3

Related Questions