2017-08-21 77 views
0

我正在使用Leaflet並使用layerselector。如何將事件綁定到leaflet layerswitcher而不將其綁定到圖層?

我的問題是,現在我定義我的層是這樣的:

var mymaps = { 
"Overlay 1": new L.geoJson(...) 
"Overlay 2": new L.geoJson(...) 
} 
L.control.layers(mymaps).addTo(mymap); 

我要的是能夠做到:

var mymaps = { 
"Overlay 1": new L.geoJson(...) 
"Overlay 2": new L.geoJson(...) 
"Overlay 3": function() { 
    console.log("Overlay 3 Selected"); 
    // call some other methods to pull in data and affect map 
    } 
} 

我該怎麼辦呢?可能嗎?

回答

0

當使用L.Control.Layers時,baselayerchangeoverlayaddoverlayremove事件在您的地圖實例上觸發。在你的情況下,你需要baselayerchange事件:

當通過圖層控件更改基礎層時觸發。

參考:http://leafletjs.com/reference-1.2.0.html#map-baselayerchange

map.on('baselayerchange', function (e) { 
    console.log(e.name); // The name of the layer that was added or removed. 
    console.log(e.layer); // The layer that was added or removed. 
});