Search code examples
pythongoogle-maps-api-3folium

How to use python folium marker save custom information?


I currently have some information like images, videos, I want to save them into the folium marker.

I am not quite sure how to add that information into this:

image = "sample.png"

folium.Marker(location = [lat, long], ??, ??).add_to(current_map)

Is there any way to achieve that? Thank you!


Solution

  • To save an image to a folium marker, the target image needs to be converted by base64. The converted image is then converted into an IFrame that can be displayed on the web. For other marker customization, please refer to the official reference.

    import folium
    import base64
    from folium import IFrame
    
    logo_png = './data/logo-stackoverflow_resize.png'
    encoded = base64.b64encode(open(logo_png, 'rb').read()).decode()
    
    lat, lon = 40.70896, -74.00680
    m = folium.Map(location=[lat, lon], zoom_start=15)
    
    html = '<img src="data:image/png;base64,{}">'.format
    iframe = IFrame(html(encoded), width=311+20, height=62+20)
    popup = folium.Popup(iframe, max_width=400)
    
    icon = folium.Icon(color="red", icon="ok")
    marker = folium.Marker(location=[lat, lon], popup=popup, icon=icon)
    marker.add_to(m)
    
    m
    

    enter image description here