Search code examples
openlayers

Set static Text to Vector in Openlayers 6


I am trying to set "Epicenter" to Vector created I am using Text , Fill and Stroke for Style then send this style to my vector but it shows nothing.

    const epicenterStyle = new ol.style.Style({
        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:3
            }),
            textAlign:'left',
            offsetX:2
        })
    });
    pointEarthquake = new ol.layer.Vector({
        source: new ol.source.Vector({
            features: [position]
        }),
        visible: true,
        title: "Evento sísmico",
        style: epicenterStyle.setText('Epicenter')
    })

Also tried on Vector style: epicenterStyle.getText().setText('Epicenter')

If i console.log(epicenterStyle) i get this: result


Solution

  • setText() updates the style, but it does not return the style

    Call setText() first then use the updated style

    epicenterStyle.getText().setText('Epicenter');
    pointEarthquake = new ol.layer.Vector({
        source: new ol.source.Vector({
            features: [position]
        }),
        visible: true,
        title: "Evento sísmico",
        style: epicenterStyle
    })
    

      <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.9.0/css/ol.css" type="text/css">
        <style>
          html, body, .map {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
          }
        </style>
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.9.0/build/ol.js"></script>
      </head>
      <body>
        <div id="map" class="map"></div>
        <script type="text/javascript">
          const map = new ol.Map({
            target: 'map',
            layers: [
              new ol.layer.Tile({
                source: new ol.source.OSM()
              })
            ],
            view: new ol.View({
              center: [0, 0],
              zoom: 4
            })
          });
          const position = new ol.Feature(new ol.geom.Point([0, 0]));
          const epicenterStyle = new ol.style.Style({
            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:3
                }),
                textAlign:'left',
                offsetX:2
            })
          });
          epicenterStyle.getText().setText('Epicenter');
          const pointEarthquake = new ol.layer.Vector({
            source: new ol.source.Vector({
                features: [position]
            }),
            visible: true,
            title: "Evento sísmico",
            style: epicenterStyle
          });
          map.addLayer(pointEarthquake);
        </script>
      </body>
    </html>