首页 > 解决方案 > Folium,切换图层时使图例消失(LayerControl)

问题描述

我在这个答案中使用 InLaw(1) 的 html 示例创建了一些图例。从那个答案中,我还设法使用来自 Branca (2) 的 MacroElement获得了这个图例,其中 template 是一个 html 宏模板。我想要的是(任何​​)这些图例根据 LayerControl 从地图中出现和消失。

(1) map.get_root().html.add_child(folium.Element( legend_html ))
(2) macro = MacroElement()
macro._template = Template(template)
m.get_root().add_child(macro)

我尝试了什么?我尝试将 html 和 MacroElement 添加到 FeatureLayer 或 Choropleth 地图,但无济于事。在网上潜伏了一段时间后,我发现了这个使用 java 脚本的示例(我猜,我对 JS 有 -0 经验)使栏不可见。但是,当我将 LinearColormap 替换为上述任何内容时,图例并没有消失。例如,使用macro(2) 中的变量,我尝试了这个:

m.add_child(BindColormap(FeatureGroup, macro))
m.add_child(BindColormap(Choropleth, macro))

为简单起见,为该示例命名的变量的名称在哪里FeatureGroup和是这样命名的。Choropleth

我不知道我还能做什么。

标签: pythonfolium

解决方案


推荐阅读