Sal
Peter Hoffmann Director Data Engineering at Blue Yonder. Python Developer, Conference Speaker, Mountaineer

beautiful leaflet markers with folium and fontawesome

TIL how to use fontawesome markers with folium.

Folium is a Python library that allows users to create and display interactive maps. The library uses the Leaflet.js library and is capable of creating powerful and visually appealing maps. Folium can be used to visualize geographical data by adding markers, polygons, heatmaps, and other geographical elements onto a map. The library is easy to use and offers a range of options for customizing the maps and the elements that are displayed on them.

Minimal marker

The minimal example just adds a marker at a specific location:

import folium
loc = [45.957916666667, 7.8123888888889]


m = folium.Map(location=loc, zoom_start=13)

folium.Marker(
    location=loc
).add_to(m)
m

Marker with a bootstrap icon

Markers can be customized through providing a Icon instance. As a default you can use bootstrap glyphicons that provide over 250 glyphs for free:

In addition you can colorize the marker. Available color names are red blue green purple orange darkred lightred beige darkblue darkgreen cadetblue darkpurple white pink lightblue lightgreen gray black lightgray .

m = folium.Map(location=loc)

folium.Marker(
    location=loc,
    icon=folium.Icon(icon="home",
                     color="purple",
                     icon_color="blue")
).add_to(m)

Marker with a fonteawesome icon

Font Awesome is a collection of scalable vector icons that can be customized and used in a variety of ways, such as in graphic design projects, websites, and applications. The icons are available in different styles, including Solid, Regular, and Brands, and can be easily integrated by adding the fa prefix

m = folium.Map(location=loc)
folium.Marker(
    location=loc,
    icon=folium.Icon(icon="tents", prefix='fa')
).add_to(m)

Extended Marker Customization with BeautifulIcons

The Leaflet Beautiful Icons is lightweight plugin that adds colorful iconic markers without images for Leaflet by giving full control of style to end user ( i.e. unlimited colors and many more...).

It ist exposed to folium via the Beautiful Icon plugin

Supported icon shapes are circle circle-dot doughnut rectangle rectangle-dot marker and the color be either one of the predefined or any valid hex code.

import folium.plugins as plugins

folium.Marker(
    location=loc,
    icon=plugins.BeautifyIcon(
                     icon="tent",
                     icon_shape="circle",
                     border_color='purple',
                     text_color="#007799",
                     background_color='yellow'
                 )
).add_to(m)

Custom Markers with a DivIcon

If you want to add more textual information, you can always use plain html with a DivIcon. A DivIcon represents a lightweight icon for markers that uses a simple div element instead of an image.

html = '''
<span style="size: 10px; background-color: lightblue; ">
<i class="fa-solid fa-tents"> </i>
Monte Rosa Hut
</span>
'''

folium.map.Marker(location=loc,
                  icon=DivIcon(
                      icon_size=(100,20),
                      icon_anchor=(5,5),
                      html=html )
                 ).add_to(m)