You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Folium自定义可拖拽图例与Fullscreen插件兼容性问题

解决Folium自定义图例在全屏模式下消失的问题

问题根源在于自定义图例被挂载在页面body根节点,而Fullscreen插件仅对地图容器(#map元素)执行全屏处理,不在该容器内的元素会被排除在全屏视图外。

修改方案

只需在图例初始化脚本中,将图例元素移动到地图容器内部,即可确保全屏时正常显示,同时保留拖拽功能:

修改模板中的<script>部分,新增一行代码:

$( function() {
    $( "#maplegend" ).draggable({
                    start: function (event, ui) {
                        $(this).css({
                            right: "auto",
                            top: "auto",
                            bottom: "auto"
                        });
                    }
                });
    // 新增:将图例移动到地图容器内
    $('#maplegend').appendTo('#map');
});

完整可运行代码

from branca.element import Template, MacroElement
import folium
from folium import plugins

m = folium.Map(location=(30, 20), zoom_start=4)

template = """
{% macro html(this, kwargs) %}

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Draggable - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

  <script>
  $( function() {
    $( "#maplegend" ).draggable({
                    start: function (event, ui) {
                        $(this).css({
                            right: "auto",
                            top: "auto",
                            bottom: "auto"
                        });
                    }
                });
    // 将图例移动到地图容器内,确保全屏时可见
    $('#maplegend').appendTo('#map');
});

  </script>
</head>
<body>

<div id='maplegend' class='maplegend'
    style='position: absolute; z-index:9999; border:2px solid grey; background-color:rgba(255, 255, 255, 0.8);
     border-radius:6px; padding: 10px; font-size:14px; right: 20px; top: 20px;'>

<div class='legend-title'>Population</div>
<div class='legend-scale'>
  <div class='legend-labels' style="display: flex; justify-content: space-between;">
    <div style="display: flex; flex-direction: column; align-items: center;">
        <span style='background:#fee5d9; opacity:0.8; width: 50px; height: 20px; display: inline-block;'></span>
        <span>(0-50)</span>
    </div>
    <div style="display: flex; flex-direction: column; align-items: center;">
        <span style='background:#fcbba1; opacity:0.8; width: 50px; height: 20px; display: inline-block;'></span>
        <span>(51-100)</span>
    </div>
    <div style="display: flex; flex-direction: column; align-items: center;">
        <span style='background:#fc9272; opacity:0.8; width: 50px; height: 20px; display: inline-block;'></span>
        <span>(101-250)</span>
    </div>
    <div style="display: flex; flex-direction: column; align-items: center;">
        <span style='background:#fb6a4a; opacity:0.8; width: 50px; height: 20px; display: inline-block;'></span>
        <span>(251-500)</span>
    </div>
    <div style="display: flex; flex-direction: column; align-items: center;">
        <span style='background:#ef3b2c; opacity:0.8; width: 50px; height: 20px; display: inline-block;'></span>
        <span>(501-750)</span>
    </div>
    <div style="display: flex; flex-direction: column; align-items: center;">
        <span style='background:#cb181d; opacity:0.8; width: 50px; height: 20px; display: inline-block;'></span>
        <span>(751-1000)</span>
    </div>
    <div style="display: flex; flex-direction: column; align-items: center;">
        <span style='background:#99000d; opacity:0.8; width: 50px; height: 20px; display: inline-block;'></span>
        <span>(> 1001)</span>
    </div>
  </div>
</div>
</div>

</body>
</html>

<style type='text/css'>
  .maplegend .legend-title {
    text-align: left;
    margin-bottom: 5px;
    font-weight: bold;
    font-size: 90%;
    }
  .maplegend .legend-scale ul {
    margin: 0;
    margin-bottom: 5px;
    padding: 0;
    float: left;
    list-style: none;
    }
  .maplegend .legend-scale ul li {
    font-size: 80%;
    list-style: none;
    margin-left: 0;
    line-height: 18px;
    margin-bottom: 2px;
    }
  .maplegend ul.legend-labels li span {
    display: block;
    float: left;
    height: 16px;
    width: 30px;
    margin-right: 5px;
    margin-left: 0;
    border: 1px solid #999;
    }
  .maplegend .legend-source {
    font-size: 80%;
    color: #777;
    clear: both;
    }
  .maplegend a {
    color: #777;
    }
</style>
{% endmacro %}"""

macro = MacroElement()
macro._template = Template(template)

m.get_root().add_child(macro)

# 添加全屏按钮                                               
plugins.Fullscreen(                                                         
        position                = "bottomleft",                                   
        title                   = "打开全屏地图",                       
        title_cancel            = "关闭全屏地图",                      
        force_separate_button   = True,                                         
    ).add_to(m) 

m

原理说明

  • Folium地图会被渲染为页面中的#map容器,Fullscreen插件的全屏效果仅作用于该容器
  • 原代码中自定义图例直接挂载在<body>节点下,不属于#map容器的子元素,因此全屏时不会被包含
  • 通过$('#maplegend').appendTo('#map')将图例移动到#map容器内部,即可让图例随地图一同进入全屏视图,同时保留拖拽功能

内容的提问来源于stack exchange,提问作者Ailurophile

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 18:59:52