[英]Hover in popup in Folium
用這樣一個簡單的例子:
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
你可以通過放置鼠標來顯示彈出窗口嗎? 這可能與大葉嗎?
你不能輕易地從 folium 做到這一點。 但由於 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 文件,找到所有標記,並為每個標記添加代碼。
更新的答案
事實證明,此功能已被放入 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
在下面的示例中,彈出窗口在鼠標懸停時打開,而不是在單擊時打開,並在用戶移開鼠標時隱藏它:
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())
你的問題是問關於大青葉,我不這么認為,但我認為你可以添加一些JavaScript(用jQuery這將會是超級容易,我懷疑),以模擬點擊事件onmouseover
或mouseenter
var test = document.getElementById("test");
test.addEventListener("mouseenter", handlerClickFunction);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.