vue-leaflet/Vue2Leaflet

How to listen layer's change?

trdmm opened this issue · 4 comments

trdmm commented

When I use LControlLayers,how can i listen layer's change?

I am a noob .

I want to use 2 map: A and B. A contains one img tile, B contains 2 tile: img tile and annotation tile.
So, I want to use LControlLayers and LTileLayer to set A&B img tile layer,and listen layer's change to show B's annotation tile. But LControlLayers has only one event: ready. 😭

Or,Is there any other better way?

mikeu commented

Hi @trdmm, the controls in Vue2Leaflet also allow you to listen to every event emitted by the underlying Leaflet components. In this case, what you'll want is to listen for the baselayerchange event from the map, something like this:

<l-map @baselayerchange="layerChanged" ...>
  <l-control-layers />
  <l-tile-layer layer-type="base" .../>
  <l-tile-layer layer-type="base" .../>
</l-map>
xlcrr commented
var layerControls = L.control.layers(null, overlays);

map.on('overlayadd', changeLayers);
map.on('overlayremove', changeLayers);

function changeLayers (layer) 
{
    ...
}
trdmm commented

Hi @trdmm, the controls in Vue2Leaflet also allow you to listen to every event emitted by the underlying Leaflet components. In this case, what you'll want is to listen for the baselayerchange event from the map, something like this:

<l-map @baselayerchange="layerChanged" ...>
  <l-control-layers />
  <l-tile-layer layer-type="base" .../>
  <l-tile-layer layer-type="base" .../>
</l-map>

Thanks 👍

Forgive me for not viewing the Leaflet documentation 😿

trdmm commented
var layerControls = L.control.layers(null, overlays);

map.on('overlayadd', changeLayers);
map.on('overlayremove', changeLayers);

function changeLayers (layer) 
{
    ...
}

Thanks good idea. 😺