Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict labeling to one label for MultiPolygon features

Tags:

openlayers-3

Default labeling in OpenLayers 3.10.1 labels each part of a MultiPolygon. I want to know if it is possible to label only the first polygon in the MultiPolygon.

like image 545
Enzo Avatar asked Feb 08 '23 16:02

Enzo


1 Answers

You can use a separate style for the label with a geometry function, which returns a single point for the label position.

var styles = [
  // Style for the label
  new ol.style.Style({
    text: new ol.style.Text({..}),
    geometry: function(feature) {
        // expecting a MultiPolygon here
        var interiorPoints = feature.getGeometry().getInteriorPoints();
        return interiorPoints.getPoint(0);
    }
  }),
  // Style for the polygons
  new ol.style.Style({
    stroke: new ol.style.Stroke({...}),
    fill: new ol.style.Fill({...})
  })
];

http://jsfiddle.net/p0acpbtg/2/

like image 181
tsauerwein Avatar answered Feb 15 '23 06:02

tsauerwein