2

用这样一个简单的例子:

import folium

map_1 = folium.Map(location=[45.372, -121.6972], zoom_start=12,
                   tiles='Stamen Terrain')
folium.Marker([45.3288, -121.6625], popup='Mt. Hood Meadows').add_to(map_1)
map_1

只需将鼠标放在上面,您就可以弹出一个弹出窗口吗?用大叶可以吗?

4

4 回答 4

3

你不能轻易地从叶中做到这一点。但由于 folium 确实创建了 LeafletJS 代码,您可以修改输出以使其工作。为此,您必须在生成的 html 中添加此答案中所述的代码:

    marker.bindPopup("Popup content");
    marker.on('mouseover', function (e) {
        this.openPopup();
    });
    marker.on('mouseout', function (e) {
        this.closePopup();
    });

如果你有很多标记,这可能会成为一项艰巨的任务,但你可以通过 python 来完成(虽然它看起来不漂亮)。伪代码:

import re

with open("map.html") as inf:
    txt = inf.read()

#Find all the markers names given by folium
markers = re.findall(r'\bmarker_\w+', txt)

for marker in markers:
   # Add the code given before to the string txt

# Save the new map
with open("new_map.html", "w") as outf:
    outf.write(txt)

此代码打开生成的 html 文件,找到所有标记,并为每个标记添加代码。

于 2017-06-02T04:59:23.830 回答
3

更新的答案

事实证明,此功能已被放入 folium 的代码库中。

只需添加tooltip参数,如下所示:

import folium

map_1 = folium.Map(location=[45.372, -121.6972], zoom_start=12, tiles='Stamen Terrain',
                   tooltip = 'This tooltip will appear on hover' # THIS
                  )
folium.Marker([45.3288, -121.6625], popup='Mt. Hood Meadows').add_to(map_1)
map_1
于 2020-12-28T18:10:29.990 回答
2

在下面的示例中,弹出窗口在鼠标悬停时打开,而不是在单击时打开,并在用户鼠标悬停时将其隐藏:

map_1.save('map_1.html')
import re
import fileinput

with open("map_1.html") as inf:
   txt = inf.read()

#Find all the markers names given by folium
markers = re.findall(r'\bmarker_\w+', txt)
markers = list(set(markers))

for linenum,line in enumerate( fileinput.FileInput("map_1.html",inplace=1) ):
    pattern = markers[0] + ".bindPopup"
    pattern2 = markers[0] + ".on('mouseover', function (e) {this.openPopup();});"
    pattern3 = markers[0] + ".on('mouseout', function (e) {this.closePopup();});"

    if pattern in line:
       print(line.rstrip())
       print(pattern2)
       print(pattern3)
    else:
       print(line.rstrip())
于 2018-05-30T01:18:52.803 回答
1

您的问题是关于folium,我不这么认为,但我认为您可以添加一些 Javascript(我怀疑使用 jquery 会非常容易),以模拟点击事件onmouseovermouseenter

  var test = document.getElementById("test");

  test.addEventListener("mouseenter", handlerClickFunction);
于 2016-12-15T20:09:08.137 回答