【问题标题】:Add text to Folium map using an absolute position使用绝对位置将文本添加到 Folium 地图
【发布时间】:2020-12-02 08:58:31
【问题描述】:

我已经在我的 Python 程序中定义了:

fig = Figure(width, height)
map = folium.Map(location=[y, x], zoom_start=2)
fig.add_child(map)

如何使用绝对位置(不是纬度/经度之一)将文本添加到我的地图? 由图形的宽度和高度的百分比定义的位置。 类似的东西

Text("Toto is my name", pos_x=0.1*width,pos_y=0.05*height)

【问题讨论】:

    标签: python folium


    【解决方案1】:

    我已经研究过了,它似乎没有那个功能,只要你有 x,y 坐标,你就可以轻松地对文本进行注释。我参考this pagethis page自定义了。

    from folium.features import DivIcon
    import folium
    
    m = folium.Map([34.0302, -118.2352], zoom_start=13)
    folium.map.Marker(
        [34.0302, -118.2352],
        icon=DivIcon(
            icon_size=(250,36),
            icon_anchor=(0,0),
            html='<div style="font-size: 20pt">Toto is my name</div>',
            )
        ).add_to(m)
    m
    

    【讨论】:

      【解决方案2】:

      这里是我的解决方案。 FloatImage 为图像完成工作...... 所以我决定把我的文字转成png,然后用这个方法

      from PIL import Image, ImageDraw, ImageFont
      W, H = (300,200)
      im = Image.new("RGBA",(W,H))
      draw = ImageDraw.Draw(im)
      msg = "pycoa.fr (data from: {})".format(mypandas.data_base)
      w, h = draw.textsize(msg)
      fnt = ImageFont.truetype('/Library/Fonts/Arial.ttf', 14)
      draw.text((0,0), msg, font=fnt,fill=(0, 0, 0))
      im.crop((0, 0,2*w,2*h)).save("pycoatextlogo.png", "PNG")
      FloatImage("pycoatextlogo.png", bottom=0, left=0).add_to(map)
      

      它并不完美,但它有效:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多