Cannot access layers via JavaScript in Lizmap 3.7

Hello,
Bonjour,

Depuis le passage à Lizmap 3.7, je ne parviens plus à accéder à mes couches métiers via JavaScript avec :

lizMap.map.getLayersByName('ma_couche')

Alors que cela fonctionnait parfaitement en Lizmap 3.5.

  • En 3.7, seules les couches techniques (fond de plan, locate, etc.) sont présentes dans lizMap.map.layers au chargement.

Il est devenu impossible d’activer dynamiquement une couche métier via JavaScript tant qu’elle n’est pas déjà visible dans le viewer.
Hi,

Since upgrading to Lizmap 3.7, I can no longer access my business layers (data layers) via JavaScript like I used to in Lizmap 3.5.

For example, this used to work in 3.5:

lizMap.map.getLayersByName('my_layer')[0].setVisibility(true);

But in 3.7, it returns undefined or an empty array.

Hi! I got the same problem and I used jquery to manage layer visibility using javascript:

if ($('[data-testid=your_layer_name]').attr("class") == 'not-visible'){
                $('#node-your_layer_name').click();

Hope this help!

Great, thanks, it works! However, it requires the layer to be visible in the legend. You then need to create a JavaScript to hide the layer.

Just some clarifications :
I would :

  • not use JQuery, as plain Javascript is OK.
  • not use data-testid which is for tests…
  • it is not needed to simulate a click anymore in the legend, there is a better javascript API.

Did you look into the documentation Adding your own JavaScript ?

For instance, from the URL provided, there is this example :

var layer = lizMap.mainLizmap.state.rootMapGroup.getMapLayerByName('buildings');

// Toggle true or false the layer in the legend
layer.checked = true;

thanks @gustry it works perfectly!

1 Like