0

I'm using the following code in Open Layers 6, and it shows the feature info, even if the layer is not visible

map.on('singleclick', function(evt){
        document.getElementById('info').innerHTML = '';
        var viewResolution = vista.getResolution();
        var url = wmsLayer3.getSource().getFeatureInfoUrl(
            evt.coordinate, viewResolution, 'EPSG:25830', {'INFO_FORMAT': 'text/html'});
        if (url) document.getElementById('info').data = url;
    });

How should it be modified to show the feature info only if the layer is visible?

3
  • You could insert if (!wmsLayer3.getVisible()) return; at the start of the function.
    – Mike
    Apr 25, 2022 at 13:56
  • Beautiful! Could you please write it as answer so I can validate it?
    – jpinilla
    Apr 25, 2022 at 14:04
  • This comment will be mostly for @Mike: Mike somehow prefers answering in comments and as a consequence many of his great answers (many with CodePen examples) get lost for general public. It's a pity but that's how it is. See the latest example of complete answer in comment with a CodePen example at gis.stackexchange.com/questions/429687/…. I gave up asking him to change comments into answers.
    – TomazicM
    Apr 25, 2022 at 15:04

1 Answer 1

1

You can check the visibility of the layer in the callback function, and exit if not visible:

map.on('singleclick', function(evt){
        if (!wmsLayer3.getVisible()) return;
        document.getElementById('info').innerHTML = '';
        var viewResolution = vista.getResolution();
        var url = wmsLayer3.getSource().getFeatureInfoUrl(
            evt.coordinate, viewResolution, 'EPSG:25830', {'INFO_FORMAT': 'text/html'});
        if (url) document.getElementById('info').data = url;
    });
1
  • :-) :-) :-) :-) :-) :-)
    – TomazicM
    Apr 25, 2022 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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