The documentation of Openlayers 6.5.0 (https://openlayers.org/en/latest/apidoc/module-ol_layer_Vector-VectorLayer.html) mentions the following property for Vector Layers:
Name | Type | Description |
---|---|---|
renderOrder | module:ol/render~OrderFunction | Render order. Function to be used when sorting features before rendering. By default features are drawn in the order that they are created. Use null to avoid the sort, but get an undefined draw order. |
It's pretty unclear, and there's no example in the doc nor in forums as far as I've checked. The code seems to show that a sort() function is applies to the features. So, I've tried the following:
...
renderOrder: function(a, b){
return {b.getGeometry().getLastCoordinate() - a.getGeometry().getLastCoordinate()};
},
...
I've tried all 4 combinations of First/Last and a/b and it doesn't change anything. Does anybody know how we can order the rendering of the features in a vector layer so that they are y-ordered? (lowest y/lat on top)
Thanks to @Mike in the comment above (and knowing I can just use getCoordinates() instead of getLastCoordinate()), I've solved the issue. I have managed to apply yordering to my features thanks to the following:
...
renderOrder: function(a, b){
return {b.getGeometry().getCoordinates()[1] - a.getGeometry().getCoordinate()[1]};
},
...
Edit after @Mike's comment below: that specific layer consisted in Point features, so getCoordinates()[1] worked nicely. With multiple geometry types, using getFirstCoordinate()[1] or getLastCoordinate()[1] would be safer.
Looking much better on the map. Am I the only one to think it should be the default behavior? It is the case on Mapbox GL JS.