Openlayers 3: add a text label to the function

I have the current setting here: a fully functional script example , and while I managed to zoom in to each polygon function, I would also like to display a centralized text label for each ... field_title variable found in the get_fields method. I don’t know how to do this, and all my search queries have come up with this article: http://openlayers.org/en/v3.3.0/examples/vector-labels.html , which I find completely confusing as I am a little new to OL!

+5
source share
1 answer

To add text to ol.Feature , you save the description in this function and set the style , which is the style function (which will receive the description from this function and show it):

 field_polygon.set('description', field_title); field_polygon.setStyle(styleFunction); function styleFunction() { return [ new ol.style.Style({ fill: new ol.style.Fill({ color: 'rgba(255,255,255,0.4)' }), stroke: new ol.style.Stroke({ color: '#3399CC', width: 1.25 }), text: new ol.style.Text({ font: '12px Calibri,sans-serif', fill: new ol.style.Fill({ color: '#000' }), stroke: new ol.style.Stroke({ color: '#fff', width: 2 }), // get the text from the feature - `this` is ol.Feature // and show only under certain resolution text: map.getView().getZoom() > 12 ? this.get('description') : '' }) }) ]; } 

Your violin .

+9
source

All Articles