1

I am using the "styleFunction" from the following example: http://openlayers.org/en/v3.9.0/examples/geojson.html

var styleFunction = function(feature, resolution){
    var properties = feature.getProperties();
    var type = feature.getGeometry().getType();
    switch (type) {
        case 'Point':
        ...return some styling...
        break;
        case 'Polygon':
        ...return some styling...
        break;
    }
}   

I am currently using only the "feature" from the callback. I can't seem to find documentation about this function on Openlayers.org so I ask here:

Are there more variables available for callback and is there a way to find out without documentation?

(I need to know the layer name the function is styling, and I can't get that from the feature alone.)

1 Answer 1

1

No, no more variables on callback. To solve your question just store the layer name on feature and then get it back on callback.

//before you add ol.Feature to ol.source.Vector
feature.set('layer-name', layerName);

Get it back on styleFunction:

feature.get('layer-name');
12
  • This is not working, because I only have 1 feature :( I will try to create a JSfiddle for it.
    – PIDZB
    Commented Oct 13, 2015 at 10:24
  • Here we go: jsfiddle.net/8hgxyayp/6 As you can see in the GeoJSON, each feature has the same properties with different values. These values represent the radius of hte circle. But I can't get to those values, because when the styleFunction runs, there is only 1 layername available...
    – PIDZB
    Commented Oct 13, 2015 at 10:43
  • I don't understand your logic. The layer name is supposed to be like an ID but you are using the key of the property, instead of value. In my first example was the value of category property. Commented Oct 13, 2015 at 10:59
  • Like var category = feature.get('desc'); in your fiddle. Commented Oct 13, 2015 at 11:01
  • Oh, I didn't say unlogical, it is just I don't understand it. Now look, all you have to do is set a unique parameter to the layer and store it into the feature Commented Oct 13, 2015 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.