0

I would like to have a tooltip for each of my layers based on their display which is done by a checkbox.

I started a function with a condition depending on the layer but I have the error "layer is not defined". How can I declare a variable that retrieves the name of my layers to be able to filter for the tooltips next?

function displayTooltip(evt) {  
    var pixel = evt.pixel;  
    var map = $('#map').data('map');    
    var feature = map.forEachFeatureAtPixel(pixel,
     function(feature, layer) {         
       return feature; 
        });

    var tooltip = document.getElementById('tooltip');

    var overlay = new ol.Overlay({
       element: tooltip,
        offset: [0, 0],
        positioning: 'bottom-left'

    });

    map.addOverlay(overlay);

    tooltip.style.display = feature ? '' : 'none';

    if (layer ===  missionsEAU | missionsEAU2) {
        overlay.setPosition(evt.coordinate);
        tooltip.innerHTML = feature.getProperties().DOCUMENT_QUOTE;

    } else if(layer ===  scot_admin){
        overlay.setPosition(evt.coordinate);
        tooltip.innerHTML = feature.getProperties().SCOT_2016;

    } else if(layer ===  epci_admin){
        overlay.setPosition(evt.coordinate);
        tooltip.innerHTML = feature.getProperties().EPCI_LATEST;

    } else if(layer ===  commune_admin){
        overlay.setPosition(evt.coordinate);
        tooltip.innerHTML = feature.getProperties().NOM_COMMUNE_LATEST;
    }
};
0

The problem is, that the variable layer is in scope of another function. Here:

var feature = map.forEachFeatureAtPixel(pixel,
 function(feature, layer) {         
   return feature; 
    });

You get only the feature from that function, you can get both of it for example like this:

var arr = map.forEachFeatureAtPixel(pixel,
 function(feature, layer) {         
   return [feature,layer]; 
    });
var feature = arr[0];
var layer = arr[1];

If you want to name your layer, it is described for example here:

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.