sanju verma
sanju verma

Reputation: 33

How to get feature information from vector layer in Openlayers 3

In case of Openlayers 2 I am able to get feature information but how to get feature information from vector layer in Openlayers 3

below code how to extract information of the feature?

 var layerWFS = new ol.layer.Vector({
  source: new ol.source.Vector({
    loader: function(extent) {
      $.ajax('http://localhost:8080/geoserver/wfs', {
        type: 'GET',
        data: {
          service: 'WFS',
          version: '1.1.0',
          request: 'GetFeature',
          typename: 'dgm:all_block_boundary_point',
          srsname: 'EPSG:3857',
          bbox: extent.join(',') + ',EPSG:3857'
        }
      }).done(function(response) {
        layerWFS
        .getSource()
        .addFeatures(new ol.format.WFS()
          .readFeatures(response));
         // console.log(response);
      });
    },
    strategy: ol.loadingstrategy.bbox,
    projection: 'EPSG:3857'
  })
}); 
map.addLayer(layerWFS);

Upvotes: 1

Views: 2527

Answers (1)

pavankguduru
pavankguduru

Reputation: 325

You can get the feature information using feature.getProperties() to get all properties or feature.get() for a specific one.

Edit: Looks like he wants to retrieve the feature info by selection. Here's OP's solution for it.

I have found solution by using ol.interaction.Select()

 var selectSingleClick = new ol.interaction.Select();
    map.addInteraction(selectSingleClick);

    map.on('singleclick', function(event){  
layerWFS.getProperties();
        layerWFS.once('precompose',function(event){
          var selectedFeatures = selectSingleClick.getFeatures();

          readFeature(selectedFeatures);

        });
    });
    function readFeature(features){

       var myfeature = features.item(0);

        console.log(myfeature.get('block_name'));
        console.log(myfeature.get('latitude'));
        console.log(myfeature.get('longitude'));
    }

Upvotes: 1

Related Questions