【问题标题】:Popup in WFS layer OpenlayersWFS 层 Openlayers 中的弹出窗口
【发布时间】:2021-06-09 16:22:15
【问题描述】:

早安。

要使用 wfs 层,使用 Leaflet 还是 openlayers 更好?

我有一个从地理服务器返回 WFS 的 openlayers 代码。但我无法在弹出窗口中显示属性。有谁能够帮助我? 谢谢

【问题讨论】:

标签: popup openlayers layer


【解决方案1】:

您可以尝试 ol-ext ol/Overlay/PopupFeature 在弹出窗口中显示特征属性。 参见示例:https://viglino.github.io/ol-ext/examples/popup/map.popup.feature.html

【讨论】:

  • 我在此链接之后发布了我的代码。你能帮帮我吗?
  • 您好,下午好。你能帮我写代码吗?我按照您指示的链接进行操作。在弹出窗口中显示“未定义”。我不知道它可能是什么。谢谢
【解决方案2】:

按照https://viglino.github.io/ol-ext/examples/popup/map.popup.feature.html 的示例,我有这段代码,其中我的 WFS 层包含 id 和 name 属性,但是,它在弹出窗口中没有显示任何内容

    var vectorSource = new ol.source.Vector({
            format: new ol.format.GeoJSON(),
            url: function(extent) {
            return 'http://localhost:8080/geoserver/teste/wfs?service=WFS&' +
              version=1.1.0&request=GetFeature&typename=teste:MYLAYER&' +
              'outputFormat=application/json&srsname=EPSG:4326&' +
              'bbox=' + extent.join(',') + ',EPSG:4326';
            },
            strategy: ol.loadingstrategy.bbox
        });
 
 
        var vector = new ol.layer.Vector({
            source: vectorSource,
            style: new ol.style.Style({
                stroke: new ol.style.Stroke({
                    color: 'rgba(0, 0, 255, 1.0)',
                    width: 2
                })
            })
        });
               
        var layers = [
            new ol.layer.Tile({source: new ol.source.OSM()}),
            vector,
        ];
        var map = new ol.Map({
            layers: layers,
             interactions: ol.interaction.defaults({ altShiftDragRotate:false, pinchRotate:false }),
            target: 'map',
            view: new ol.View({
                center: ol.proj.fromLonLat([-46.444137, -23.713596]),
                zoom: 12
            })
        });
        
  var select = new ol.interaction.Select({
    hitTolerance: 5,
    multi: true,
    condition: ol.events.condition.singleClick
  });
 map.addInteraction(select);

  var popup = new ol.Overlay.PopupFeature({
    popupClass: 'default anim',
    select: select,
    canFix: true,
    template: {
        title: 
          function(f) {
            return f.get('nome')+' ('+f.get('id')+')';
          },
        attributes: // [ 'id', 'nome' ]
        {
          'nome': { title: 'Nome' },
          'id': { title: 'Id' },
        
        }
    }
  });
 map.addOverlay (popup);

【讨论】:

  • 这是一个工作示例:codepen.io/viglino/pen/WNpaeag
  • 您不必设置弹出模板,如果没有,它将显示所有属性。修复后,您可以尝试使用模板设置弹出窗口标题和要在弹出窗口中看到的属性...
  • 谢谢@ViglinoJean-Marc。我按照示例 codepen.io/viglino/pen/WNpaeag 并设法看到一个弹出窗口。一个问题:是否可以在弹出窗口中显示图像或带有蒙版甚至框架的页面链接?
  • 您也可以使用模板将属性格式化为 HTML 图像或链接: `` var popup = new ol.Overlay.PopupFeature({ popupClass: 'default anim', canFix:真,选择:选择,模板:{属性:{'img':{格式:函数(val){返回''; }} } } }); ```
  • @ViglinoJean-Marc 谢谢。我会尝试,我会说它是否有效。感谢您分享您的代码并帮助我解决问题。
【解决方案3】:

这是弹出代码。我有 3 层:layer1、layer2 和 layer3。 对于 layer1,ID 我想显示为 ID。对于 layer2,我想将 ID 显示为 CODE,而对于其他层,我不想显示 ID 属性。 我应该如何更改模板?谢谢

 var popup = new ol.Overlay.PopupFeature({
    popupClass: 'default anim',
    select: select_interaction,
    canFix: true,
    template: {
        title: 
            function(f) {
                return f.get('NAME')+' ('+f.get('ID')+')';
            },
        attributes: 
        {
            'ID': { title: 'ID' },
            // with prefix and suffix
            'POP': { 
                title: 'População',  // attribute's title
                before: '',           // something to add before
                format: ol.Overlay.PopupFeature.localString(),  // format as local string
                after: ' hab.'        // something to add after
            }, 
        }
    }
});

【讨论】:

  • @ViglinoJean-Marc 你能帮帮我吗?
  • 你必须使用一个函数,根据特征属性返回你想要使用的模板......
  • @ViglinoJean-Marc 我不明白。是否可以为每一层创建一个模板?如果是,如何?谢谢你
【解决方案4】:

@user12538529
您必须为每个案例创建一个函数并返回模板:

// Create templates
var templateID = { ... }; 
var templateCODE = { ... };
// Popup with a template function
var popup = new ol.Overlay.PopupFeature({
    popupClass: 'default anim',
    select: select_interaction,
    canFix: true,
    template: function(feature) {
      var prop = feature.getProperties();
      // Test if has property ID
      if (prop.hasOwnProperty('ID')) return templateID;
      // or property CODE
      else if (prop.hasOwnProperty('CODE')) return templateCODE;
    }
});

【讨论】:

  • 谢谢。我可以测试它是否是 layer1、layer2、layer3 等,而不是测试属性,例如 ID、CODE?因为在几层中我有 ID,但在一层中,我会将其显示为 ID,在另一层中显示为代码,而在其他层中我可能不想显示它。我想为要添加的每一层构建一个模板。有可能吗?
  • 您只能访问要素而不是图层,但您可以在添加时注册图层:layer.getSource ().on('addfeature', function (f){ f.layer = layer; }) 您可以访问要素图层...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多