How can I add a legend to a folium map?
中的代码创建自定义可拖动图例。
Fullscreen folium plugin
后,在全屏模式下,可拖动图例消失。
有没有办法使用 folium 全屏插件以全屏模式显示图例?
可复制代码:
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"
});
}
});
});
</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)
# Add the full screen button.
plugins.Fullscreen(
position = "bottomleft",
title = "Open full-screen map",
title_cancel = "Close full-screen map",
force_separate_button = True,
).add_to(m)
m
我也遇到了这个问题,并深入研究了全屏插件的
fullscreen.py
源代码。
所以,这个插件在这里使用默认的CSS:
default_css = [
(
"Control.FullScreen.css",
"https://cdn.jsdelivr.net/npm/[email protected]/Control.FullScreen.css",
)
]
.leaflet-container:-webkit-full-screen { width: 100% !important; height: 100% !important; z-index: 99999; }
在链接中,我们可以看到
z-index
是99999。因此,将此div标签中的z-index调整为更高的数字(例如100000)。
<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;'>
这个对我有用。