[GIS] Get layer name by click on features OpenLayers3

featureslayersopenlayersselect

I want to add selec interactions to my OL3 map.
Adding two different select interactions like this is not working. Only the second one works.
Like this:

        var select = new ol.interaction.Select({
        layers: [clusters]
    });
select.getFeatures().on('add', function (e) {alert("orange")});
    map.addInteraction(select);
    var select2 = new ol.interaction.Select({
        layers: [clustersSarv]
    });
select2.getFeatures().on('add', function (e) {alert("blue")});
    map.addInteraction(select2);

Now I am trying to do something like this, but I am unable to get layer on which is clicked, to define different actions for different layers.

var select = new ol.interaction.Select({
        layers: [clusters,clusters2]
    });
select.getFeatures().on('add', function (e) {
   console.log(e.element);

// if layer is clusters  alert("orange")});
    map.addInteraction(select);

Any suggestions? Main question is how to get a layer name by clicking on layer's features.

Best Answer

To answer your question about grabbing the layer from a clicked feature, you can do the following:

map.on(ol.MapBrowserEvent.EventType.SINGLECLICK, function (e) {
  map.forEachFeatureAtPixel(e.pixel, function (feature, layer) {
    ...
  });
}