【问题标题】:Hover in popup in Folium悬停在 Folium 的弹出窗口中
【发布时间】:2016-12-12 07:08:10
【问题描述】:

举个简单的例子:

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

你可以通过鼠标向上弹出一个弹出窗口吗? 叶子可以吗?

【问题讨论】:

    标签: python folium


    【解决方案1】:

    更新答案

    事实证明,这个功能已被放入 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
    

    【讨论】:

      【解决方案2】:

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

          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 文件,找到所有标记,并为每个标记添加代码。

      【讨论】:

        【解决方案3】:

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

        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())
        

        【讨论】:

          【解决方案4】:

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

            var test = document.getElementById("test");
          
            test.addEventListener("mouseenter", handlerClickFunction);
          

          【讨论】:

          • 是的,我认为folium 做不到。另一种解决方法是直接使用leaflet
          • 是的,很可能,但很抱歉,我不知道 :)
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-12-19
          • 1970-01-01
          • 1970-01-01
          • 2015-11-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多