Using OpenLayers 4.2.0.
I have a feature with two geometries:
var feature = new ol.Feature({
geometry: new ol.geom.Polygon(polyCoords),
labelPoint: new ol.geom.Point(labelCoords),
name: 'My Polygon'
});
Is it possible to render all of them at a time and style each geometry individually? Particularly I need to render the geometry and display the name in the labelPoint.
If I set a style to the feature it is applied to the geometry only. See this fiddle.
I also tried it via geometry collection
var feature = new ol.Feature({
geometry: new ol.geom.GeometryCollection([
new ol.geom.Polygon(polyCoords),
new ol.geom.Point(labelCoords)
]),
name: 'My Polygon'
});
but in this case the name is rendered twice. See this fiddle.
Best Answer
Instead of using a geometry collection, use features with two geometry columns. Something like
Then create two styles, one for the main geometry and one for the second one (
'geometry: point'
):