Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use folium.icon with fontawesome

I am looking to use a custom fontawesome icon, thx.

I would like to change the icon from folium.icon using fontawesome icons.

For example, I want to change this:

    import folium

    m = folium.Map(location=(25.0431, 121.539723), zoom_start=12,tiles='Cartodb Positron')

    folium.Marker(
        location=[25.0431, 121.539723], 
        icon=folium.Icon(color="red",icon="fa-truck", prefix='fa')).add_to(m)

    m

To a burger icon from fontawesome as shown below:

    folium.Marker(
        location=[25.0431, 121.539723], 
        icon=folium.Icon(color="red",icon="fa-hamburger", prefix='fa')).add_to(m)

But it does not work for me!

Many thanks!!!!

like image 765
Doeiqw Dwe Avatar asked Oct 29 '19 12:10

Doeiqw Dwe


2 Answers

Revised

My earlier response neglected this issue with Folium and Leaflet: icons added in Fontawesome v5 do not currently render in Folium or Leaflet, upon which Folium is derived. Fonts that were part of Fontawesome v4, such as "truck" work just fine as you implemented. So you'll have to wait on "hamburger" or find another marker in the Fontawesome v4 list that does work.

Remember, you can always use Bootstrap icons as an alternative if you can't find what you want with Fontawesome.


The info provided below only valid for Fontawesome v4.x icons

Welcome! You should be able to render the icon with a slight modification to your icon constructor. In normal usage, the icon argument will point to standard glyphicons from Bootstrap. If you want to use Fontawesome icons, you put in the icon's name without the prefix (e.g. just "hamburger" without "fa-" in front), then add the prefix keyword argument for Fontawesome, which is fa.

So in your case it would look like this:

folium.Marker(
    location=[25.0431, 121.539723], 
    icon=folium.Icon(color="red",icon="hamburger", prefix='fa')
).add_to(m)

See this question as well.

like image 87
mayosten Avatar answered Sep 24 '22 16:09

mayosten


I think this problem happens because folium use FontAwesome 4 while hamburger is an icon available after FontAwesome 5.7. So the solution is either to use the fonts in FontAwesome 4 icon list, or to fork Folium to update the library.

For me, at least folium.Icon(color='red',icon="car", prefix='fa') works.

like image 33
natsuapo Avatar answered Sep 22 '22 16:09

natsuapo