简体   繁体   中英

Add text to Folium map using an absolute position

I have defined in my Python program:

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

How can I add text to my map using absolute position (not latitude/longitude one)? A position defined by a percentage of the width and the height of the Figure. Something like

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

I've looked into it and it doesn't seem to have that feature, as long as you have the x,y coordinates you can easily annotate the text. I've customized it by referring to this page and this 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

在此处输入图像描述

here my solution. FloatImage do the job for an image... so I have decided to convert my text into png and then use this method

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)

It is not perfect, but it works:) 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM