jvanulde
jvanulde

Reputation: 43

Bound popup removed when layer changed in control

I have a map with a layer control that has overlays specified in the baselayer parameter:

var overlays = {
    'Layer 1': mylayer1,
    'Layer 2': mylayer2
};

L.control.layers( overlays, null, { collapsed: false } ).addTo( map );

I specify my layers as follows:

var mylayer1 = L.esri.featureLayer({
    url: 'https://.../MapServer/5'
}).on( 'load', function ( e ) {
    ...
}).on( 'loading', function ( e ) {
    ...
}).bindPopup( function ( layer ) {
    return L.Util.template( '<p>{_score}</p>', layer.feature.properties );
});

The issue is that when I change layers in the control the bindPopup event no longer gets called.

It's almost like the layer z-index is not updated. Would appreciate any insight on how I can address this.

See: https://codepen.io/jvanulde/pen/LYyOWZo

Upvotes: 0

Views: 212

Answers (1)

Grzegorz T.
Grzegorz T.

Reputation: 4153

I see no one has given an answer. A little around, but it works.
You add the id: x to each layer. Later in the loop you check which layer is active, and all the rest of the layers you add the style display: none.

window.addEventListener('DOMContentLoaded', () => {
  let tiles = L.tileLayer('//{s}.tile.osm.org/{z}/{x}/{y}.png', {
    attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors, Points &copy 2012 LINZ'
  });

  let l1 = L.esri.featureLayer({
    url: 'https://maps-cartes.services.geo.ca/server_serveur/rest/services/NRCan/nhsl_en/MapServer/1',
    id: 0, // required
    simplifyFactor: 0.25,
    precision: 5,
    fields: ['OBJECTID'],
    renderer: L.canvas()
  }).bindPopup(function(layer) {
    return L.Util.template('<p>Layer 1: <strong>{OBJECTID}</strong></p>', layer.feature.properties);
  });

  let l2 = L.esri.featureLayer({
    url: 'https://maps-cartes.services.geo.ca/server_serveur/rest/services/NRCan/nhsl_en/MapServer/2',
    id: 1, // required
    simplifyFactor: 0.25,
    precision: 5,
    fields: ['OBJECTID'],
    renderer: L.canvas()
  }).bindPopup(function(layer) {
    return L.Util.template('<p>Layer 2: <strong>{OBJECTID}</strong></p>', layer.feature.properties);
  });

  let map = L.map('map', {
    center: [49.2827, -123.1207],
    zoom: 12,
    layers: [tiles]
  });

  let overlays = {
    'Layer 1': l1,
    'Layer 2': l2
  };

  L.control.layers(overlays, null, {
    collapsed: false
  }).addTo(map);

  l1.addTo(map);

  map.on('baselayerchange', function(e) {
    const layersCanvas = document.querySelectorAll('.leaflet-overlay-pane > canvas');

    layersCanvas.forEach((layer, index) => {
      layer.style.display = '';
      if (index !== e.layer.options.id) {
        layer.style.display = 'none';
      }
    });
  });
});
html {
  height: 100%;
}

body {
  min-height: 100%;
  margin: 0;
  padding: 0;
}

#map {
  width: 100%;
  height: 100vh;
}
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<script src="https://unpkg.com/[email protected]/dist/esri-leaflet.js"></script>
<div id="map"></div>

Upvotes: 0

Related Questions