如何找到以前在功能上设置的样式文本 - OL3

How to find style text that was previosly set on a feature - OL3

使用 OL3,我动态设置文本样式:

var myLayer = new ol.layer.Vector({
            source: mySource,
            style: function (feature, resolution) {
                var style = new ol.style.Style({
                    text: new ol.style.Text({
                        text: setText(feature)
                    })
                });

                return [style];
            }
        });

我正在尝试稍后阅读文本中存储的内容:

text: setText(feature)

我正在尝试检索点击事件中的文本,但不确定如何在功能样式下访问 属性(功能是来自包含点击功能的事件的变量):

// Get current display text
var currentFeatureStyle = feature.getStyle();

但是当我这样做时,我得到一个空的 currentFeatureStyle。

还尝试循环遍历该功能:

for (var fid in feature)
{
//what to look for to extract the feature text?

但不确定要查找什么来提取特征文本。任何帮助从功能中取回功能文本的帮助将不胜感激。

谢谢

如果有人能想出更正确的答案,请务必在下方post。我创建了一个 "workaround",因为我无法从该功能中检索文本。解决方法包括以下内容:

  1. 在原来设置文本的方法里面,添加如下内容:

    function setText(feature)
    {
    
        // First do your normal stuff (set the text of the feature)
        var output = "myFeatureText";
    
        // Here is the workaround step 1:
        // Create a property and set it to the text
        feature.displayText = output;
    
        // Return back value for the setting of the style text
        return output;
    }
    

现在文本也保存在一个名为 displayText 的 属性 中。注意:displayText是编出来的属性,在openlayers中是没有的

  1. 第2步是检索您在第1步中创建的属性以获取显示文本。在这个例子中,我们从 singleclick 中检索该功能,但它可以来自您使用该功能的任何其他地方:

    map.on('singleclick', function (evt) {
    
    var feature = map.forEachFeatureAtPixel(evt.pixel,
                    function (feature, layer) {
                        return feature;
                    });
    
    // Here is the workaround step 2:
    
    // Get "display text" custom field for this feature
    
    var displayText = feature.displayText;
    

    .

仅此而已。必须有一种 "correct" 方法可以从功能的样式中检索文本,但我不知道。如果有人知道如何 post 你的答案。