我怎么在 folium 中添加文本框(其行为与 LayerControl 面板差不多)?

huangapple go评论69阅读模式
英文:

How can I add a text box to folium (with more or less the same behavior as the LayerControl panel)?

问题

我想在folium地图上添加一个文本框,以提供有关源数据以及如何使用地图作为资源的更多信息。如果可能的话,我希望它的外观和行为几乎与LayerControl功能相同(如果能够折叠,那将是额外的奖励)。

(顺便说一句,这其实是我最初想要的解决方法:在LayerControl面板中的每个项目旁边悬停在图标上以显示弹出信息框,但我怀疑这会更加要求严格,因为它涉及修改folium.LayerControl)。

到目前为止,我只能找到解决方案在地图上添加标记,但这并不真正做到与简单文本框相同的事情。其他解决方案涉及在地图上特定位置添加文本,但我需要文本框显示在用户导航到的任何位置。这个是我找到的与我需求最接近的解决方案,但它只是在地图上方添加文本,并且我不确定修改HTML代码是否会简化到与在特定位置添加文本相同的解决方案。

英文:

I want to add a text box to a folium map that gives more information about the source data and how the map can be used as a resource. If possible, I want it to look and behave almost identically with the LayerControl function (and a bonus would be if it were collapsible too).

(This is incidentally my attempted work-around for what I initially had wanted: the ability to hover over an icon next to each item in the LayerControl panel to display a popup infobox, but I suspect that this would be far more demanding, since it involves modifying folium.LayerControl).

So far, I have only been able to find solutions that add a marker to the map, but this isn't really doing the same thing as a simple text box. Other solutions involve adding text at a specific location on the map, but I need the textbox to be displayed wherever the user navigates. This is the closest solution I've come across to what I need, but this only adds text above the map, and I'm not sure that modifying the html code wouldn't be reducible to the same solution as adding text at a specific location.

答案1

得分: 3

# 回答摘要
你可以使用[branca](https://github.com/python-visualization/branca)来注入HTML和CSS可用于添加自定义图例也可以拖动)。

这是一个基于这个branca [演示nbviewer](https://nbviewer.org/gist/talbertc-usgs/18f8901fc98f109f2b71156cf3ac81cd)的基本示例

## 预览

![结果预览](https://www.pixenli.com/image/D2M7kEsT)

你可以看到我甚至可以定制浏览器标签的标题如果你将文件托管在GitHub Pages或其他地方你可以在标签上使用项目名称添加图标等

## 代码

```python
# 代码部分不翻译

我为乐趣添加了毛玻璃效果,但你可以用它做很多事情。


<details>
<summary>英文:</summary>

# Answer gist
You can use [branca](https://github.com/python-visualization/branca) to inject HTML and CSS (can be used to add a custom legend, draggable too).

Here&#39;s a basic example based on this branca [demo nbviewer](https://nbviewer.org/gist/talbertc-usgs/18f8901fc98f109f2b71156cf3ac81cd)

## Preview

![result preview](https://www.pixenli.com/image/D2M7kEsT)

You can see that I can customize even the title of the tab of the browser. In case your hosting the file on GitHub Pages or somewhere else, you can have your project name on the tab, add an icon... etc.

## Code

```python

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

# main map
m = folium.Map(location=[35, 38], tiles=None, zoom_start=3)

# adding a basemap
dark_basemap = folium.TileLayer(&quot;cartodbdark_matter&quot;, name=&quot;Dark Theme Basemap&quot;).add_to(m)

# Injecting custom css through branca macro elements and template, give it a name
textbox_css = &quot;&quot;&quot;
{% macro html(this, kwargs) %}
&lt;!doctype html&gt;
&lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
    &lt;title&gt;Textbox Project&lt;/title&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css&quot;&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css&quot; integrity=&quot;sha512-MV7K8+y+gLIBoVD59lQIYicR65iaqukzvf/nwasF0nqhPay5w/9lJmVM2hMDcnK1OnMGCdVK+iQrJ7lzPJQd1w==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;/&gt;
    &lt;script src=&quot;https://code.jquery.com/jquery-1.12.4.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;https://code.jquery.com/ui/1.12.1/jquery-ui.js&quot;&gt;&lt;/script&gt;

    &lt;script&gt;
      $( function() {
        $( &quot;#textbox&quot; ).draggable({
          start: function (event, ui) {
            $(this).css({
              right: &quot;auto&quot;,
              top: &quot;auto&quot;,
              bottom: &quot;auto&quot;
            });
          }
        });
      });
    &lt;/script&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;div id=&quot;textbox&quot; class=&quot;textbox&quot;&gt;
      &lt;div class=&quot;textbox-title&quot;&gt;Textbox (draggable)&lt;/div&gt;
      &lt;div class=&quot;textbox-content&quot;&gt;
        &lt;p&gt;You can put whatever content here.&lt;br&gt;You can create as many elements and positon them all over the map.&lt;/p&gt;
        &lt;p&gt;You can delete the script that makes it draggable and all script links that come with it.&lt;/p&gt;
        &lt;p&gt;You can add a link to a local CSS file in the head and past the css in it instead of here.&lt;/p&gt;
        &lt;p&gt;You can find a way to make it collapsible with JS or CSS, it&#39;s a normal HTML div after all.&lt;/p&gt;
      &lt;/div&gt;
    &lt;/div&gt;
 
&lt;/body&gt;
&lt;/html&gt;

&lt;style type=&#39;text/css&#39;&gt;
  .textbox {
    position: absolute;
    z-index:9999;
    border-radius:4px;
    background: rgba( 28, 25, 56, 0.25 );
    box-shadow: 0 8px 32px 0 rgba( 31, 38, 135, 0.37 );
    backdrop-filter: blur( 4px );
    -webkit-backdrop-filter: blur( 4px );
    border: 4px solid rgba( 215, 164, 93, 0.2 );
    padding: 10px;
    font-size:14px;
    right: 20px;
    bottom: 20px;
    color: orange;
  }
  .textbox .textbox-title {
    color: darkorange;
    text-align: center;
    margin-bottom: 5px;
    font-weight: bold;
    font-size: 22px;
    }
&lt;/style&gt;
{% endmacro %}
&quot;&quot;&quot;
# configuring the custom style (you can call it whatever you want)
my_custom_style = MacroElement()
my_custom_style._template = Template(textbox_css)

# Adding my_custom_style to the map
m.get_root().add_child(my_custom_style)

# Adding the layer control
folium.LayerControl(collapsed=False).add_to(m)

# save the map as html
m.save(&quot;m.html&quot;)

# opens in default browser
webbrowser.open(&quot;m.html&quot;)

I added glassmorphism effect for the fun but you can do a lot with it.

huangapple
  • 本文由 发表于 2023年2月18日 21:13:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493570.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定