我是 Leaflet/JavaScript 的新手,并且一直在努力让图例仅在从图层控件中选择特定图层时才显示地图。我有三层,其中一层我希望没有图例,另外两层有相应的图例。我遇到了一个例子,但未能使其工作:
// Add and remove legend from layers
map.on('overlayadd', function (eventLayer) {
// Switch to the Permafrost legend...
if (eventLayer.name === 'Permafrost') {
this.removeControl(legend1);
legend2.addTo(this);
} else { // Or switch to the treeline legend...
this.removeControl(legend2);
legend1.addTo(this);
}});
我用具体的例子创建了一个jsfiddle:
http://jsfiddle.net/gerlis/T8DHb/3/ http://jsfiddle.net/gerlis/T8DHb/3/
任何指导将不胜感激。
您的代码只需要进行一些更改。工作小提琴:http://jsfiddle.net/T8DHb/8/ http://jsfiddle.net/T8DHb/8/
当您更改基础层时,触发的事件不是“overlayadd”,而是“baselayerchange”:
map.on('baselayerchange', function (eventLayer) {
您应该只添加要为默认基础图层显示的图层来映射。我添加了永久冻土。
此外,您应该只向地图添加您想要与默认基础图层一起使用的图例。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)