单张添加/删除图层选择图例 [英] Leaflet add/remove legends with layer selection

查看:152
本文介绍了单张添加/删除图层选择图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Leaflet / JavaScript的新手,并且一直在努力获取图例,只有当从图层控件中选择了特定图层时才会显示地图。我有三层,其中一个我想没有传说,另外两个有相应的传说。我遇到了一个例子,但是没有能够使它工作:

I'm new to Leaflet/JavaScript and have been struggling to get legends a map to show only when a specific layers is selected from the layer control. I have three layers, one of which I would like to have no legend and two others that have a corresponding legend. I came across an example, but have not been able to make it work:

// 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:

I created a jsfiddle with the specific example:

http://jsfiddle.net/gerlis/T8DHb/3/

任何指导都将不胜感激。

Any guidance would be greatly appreciated.

推荐答案

您的代码只需一对的变化。工作小提琴:
http://jsfiddle.net/T8DHb/8/

Your code needed just a couple of changes. Working fiddle: http://jsfiddle.net/T8DHb/8/

当您更改基础层时,即使触发的不是overlayadd,它是baselayerchange:

When you change the base layer, the even fired is not 'overlayadd', it is 'baselayerchange':

map.on('baselayerchange', function (eventLayer) {

您只应添加映射要为默认基础层显示的图层,我添加了PermaFrost。

You should only add to map the layer which you want to show for the default base layer. I added PermaFrost.

此外,您应该只向图中添加图例你想要使用默认的基础层。

Also, you should only add to the map the legend which you want to go with the default base layer.

这篇关于单张添加/删除图层选择图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆