Monday, 6 January 2020

StyleFunction Openlayers-3 - multiple callback variables?


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.)



Answer



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');

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...