3

I'm getting a vector layer from a WMS server, but I don't know which fields are included in it. In previous version feature object had a attributes collection but I can't find it now.

Is there a way of searching for feature attributes?

Right now I can use Feature.get('Name') and can get the value, but only if I know 'Name' is an attribute. Now imagine I do find a [Name] field or something similar.

Is possible do a getFeaturesByAttribute('Name', strName);

Also available in previous version. But can't find it in OpenLayers 3.

3

getKeys() or getProperties() should be what you are looking for.

2

For my application, I wanted to show attributes of polygon feature as a tooltip and I used following code to get attributes.

var info = $('#ttt');

info.tooltip({
    animation: false,
    trigger: 'manual'
});

var highlight;
var displayFeatureInfo = function(pixel) {
    var currentZoom = map.getView().getZoom();
    var feature = map.forEachFeatureAtPixel(pixel, function(feature, layer) {
        return feature;
    });

    if (feature !== highlight) {...}
    info.css({...});

    if (feature) {
        var att = feature.getProperties();
        var attStr = att.p_id + "\n" + att.pname +
                    "\n" + att.area +
                    "\n" + att.perimeter + "\n";

        info.tooltip('hide')
        .attr('data-original-title', attStr)
        .tooltip('fixTitle')
        .tooltip('show');
    } else {
        info.tooltip('hide');
    }
};

$(map.getViewport()).on('mousemove', function(evt) {
    displayFeatureInfo(map.getEventPixel(evt.originalEvent));
});

map.on('pointermove', function(evt) {
    if (evt.dragging) {
        info.tooltip('hide');
        return;
    }
    var pixel = map.getEventPixel(evt.originalEvent);
    displayFeatureInfo(pixel);
    displayFeatureInfo(map.getEventPixel(evt.originalEvent));
});

for style of tooltip:

#ttt {
    position: absolute;
    height: 1px;
    width: 1px;
    z-index: 100;
}
.tooltip.in {
    opacity: 1;
    filter: alpha(opacity=100);
}
.tooltip.top .tooltip-arrow {border-top-color: white;}
.tooltip-inner {border: 2px solid white;}

and finally add tooltip:

    <div id="map" class="map">
        <div id="ttt"></div>
    </div>
-2
findbyfid: function(fid) {
                //Get all vector layers to search FID
                var alllayers = app.olmap.getLayers();
                if (alllayers.length === 1) {   //1 bcos atleast one tile layers is there.
                    alert("Please select vector layers on legend");
                    return;
                }

                alllayers.forEach(function(layer,i) {
                    if (layer instanceof ol.layer.Vector) {
                        var prop = layer.getProperties();
                        var src = layer.getSource();
                        src.forEachFeature(function(feature) {
                            var att = feature.get("mt_fid");
                            if (att == fid) {
                                var dataExtent = feature.getGeometry().getExtent();
                                var view = app.olmap.getView();
                                view.fitExtent(dataExtent, (app.olmap.getSize()));
                                highlight.addFeature(feature);
                                return;                                
                            }
                        })

                    }

                }) 
            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.