在选择时将wfs属性显示为标签

问题描述 投票:1回答:3

我试图在我的openlayer3应用程序中显示来自geoserver的wfs gml图层的属性作为标签。我成功将标签作为文本但无法访问特定属性“名称”。鉴于我正在使用的代码。

var sourceWFS = new ol.source.Vector({
    loader: function (extent) {
        $.ajax('..../geoserver/harry/ows?', {
            type: 'GET',
            data: {
                service: 'WFS',
                version: '1.1.0',
                request: 'GetFeature',
                typename: 'ABC',
                srsname: 'EPSG:3857',
                geometryField:'geometry',
                bbox: extent.join(',') + ',EPSG:3857'
            }

        }).done(function (response) {
            sourceWFS.addFeatures(formatWFS.readFeatures(response));
        });
    },
    strategy: ol.loadingstrategy.tile(ol.tilegrid.createXYZ()),
    strategy: ol.loadingstrategy.bbox,
    projection: 'EPSG:3857',

});
var layerWFS = new ol.layer.Vector({
    source: sourceWFS
});

var interaction;

var interactionSelectPointerMove = new ol.interaction.Select({
    condition: ol.events.condition.pointerMove
});

var interactionSelect = new ol.interaction.Select({


    style: new ol.style.Style({ 

            stroke: new ol.style.Stroke({
                color: 'rgba(255,0,0,1.0)',
                width: 1
            }),
            fill: new ol.style.Fill({
                color: 'rgba(255,0,0,0.5)'
            }),
            text: new ol.style.Text({
                text:("abcd")
            })


        })
});

var interactionSnap = new ol.interaction.Snap({
    source: layerWFS.getSource()
});

我正在选择abcd作为标签

styles label openlayers-3 geoserver
3个回答
1
投票

您需要一个样式函数来设置样式中的文本,而不是您希望显示的任何要素属性

var selectStyle = new ol.style.Style({ 

            stroke: new ol.style.Stroke({
                color: 'rgba(255,0,0,1.0)',
                width: 1
            }),
            fill: new ol.style.Fill({
                color: 'rgba(255,0,0,0.5)'
            }),
            text: new ol.style.Text({
                text:("abcd")
            })

        });



var interactionSelect = new ol.interaction.Select({

    style: function(feature) {

            selectStyle.getText().setText(feature.get('name'));
            return selectStyle;

        }

});

0
投票

默认情况下,您不会获得任何被GML属性“隐藏”的属性。最常见的“缺失”属性是name和id。您可以通过检查WFS服务页面中的Override GML Attributes来了解客户端请求的GML版本,从而转换此(标准符合)行为。

relevant part of WFS service settings page


0
投票

你实际显示的是“abcd”字符串本身而不是“abcd”属性的值。要访问ol.Style中的某些要素属性值,您必须使用StyleFunction,如下所示:

    style: function(feature, resolution){
             return new ol.style.Style({ 
                    stroke: new ol.style.Stroke({
                        color: 'rgba(255,0,0,1.0)',
                        width: 1
                    }),
                    fill: new ol.style.Fill({
                       color: 'rgba(255,0,0,0.5)'
                    }),
                    text: new ol.style.Text({
                       text: feature.get("abcd");
                    })
              })
           }
© www.soinside.com 2019 - 2024. All rights reserved.