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

Openlayers 3 如何查找以前在功能上设置的样式文本-OL3,openlayers-3,Openlayers 3,使用OL3,我在样式上动态设置文本: var myLayer = new ol.layer.Vector({ source: mySource, style: function (feature, resolution) { var style = new ol.style.Style({ text: new ol.style.Text({

使用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)
我试图检索单击事件的文本,但不确定如何在功能样式下访问该属性(功能是包含单击功能的事件中的变量):

但是当我这样做的时候,我会得到一个null currentFeatureStyle

还尝试循环使用该功能:

for (var fid in feature)
{
//what to look for to extract the feature text?
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;
但不确定要寻找什么来提取特征文本。如果您能帮助从功能中获取功能文本,我们将不胜感激


谢谢

如果有人能想出一个更正确的答案,请尽一切可能在下面发布。我已经创建了一个“变通方法”,因为我无法从功能中检索文本。解决方案包括以下内容:

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;
}
  • 在最初设置文本的方法中,添加以下内容:

    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中不存在

  • 步骤2是检索您在步骤1中创建的属性以获取显示文本。在本例中,我们从singleclick检索该功能,但它可以从您使用该功能的任何其他位置:

    for (var fid in feature)
    {
    //what to look for to extract the feature text?
    
    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;
    

  • 这就是一切。必须有一个“正确”的方法从功能的样式检索文本,但我不知道它。如果有人知道如何通过各种方式发布你的答案