【问题标题】:Set static Text to Vector in Openlayers 6在 Openlayers 6 中将静态文本设置为矢量
【发布时间】:2021-12-27 18:56:35
【问题描述】:

我正在尝试将“Epicenter”设置为创建的 Vector,我使用 TextFillStroke 作为 Style,然后将此 style 发送到我的向量,但它什么也没显示。

    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')
    })

还尝试了 Vector style: epicenterStyle.getText().setText('Epicenter')

如果我 console.log(epicenterStyle) 我得到这个:

【问题讨论】:

    标签: openlayers


    【解决方案1】:

    setText() 更新样式,但不返回样式

    先调用setText(),然后使用更新后的样式

    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>

    【讨论】:

    • 尝试作为您的示例,但没有得到任何更改,还尝试使用类似 let updatedStyle = epicenterStyle.getText().setText('Epicenter'); 的变量,然后在 Vector 上将其用作 style,但再次一无所获
    • 我添加了一个工作代码 sn-p
    • 谢谢,我忘了我用的是另一个style,刚刚添加到我的第一个样式中,它起作用了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多