1

I have a question, I need to make more than one GetFeatureInfo (obtaining information under the layer that you click), and for this I need the name of the layer I click on it to get the information.

Anyone know how to get the name of this layer?

This is my getFeatureInfo code:

map.on('click', function(evt) {
          var pixel = map.getEventPixel(evt.originalEvent);

          var url = WMS2.getSource().getGetFeatureInfoUrl(
              evt.coordinate, viewResolution, 'EPSG:3857',
              {'INFO_FORMAT': 'application/json',
               'propertyName': 'referencia'});


          if (url) {
            var parser = new ol.format.GeoJSON();
             $.ajax({url: url,

                    dataType: 'json',
                    success:function(response) {

              var result = parser.readFeatures(response);

              if (result.length) {
                var info2 = [];
                for (var i = 0, ii = result.length; i < ii; ++i) {
                  info2.push(result[i].get('referencia'));
                }
                document.getElementById("Getfeatureinfo").value = info2;

              } else {
                document.getElementById("Getfeatureinfo").value = info2;

              }
            }});
1

You cannot directly get the name of the layers. But each layers first column has the layer name.feature_id

Just try to parse the json and get the first column in to into string then by use string.split function

for(var i in result.features){
    var id = result.features[i].id;
    var layer_name = id.split(".")[0];
}

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.