问题
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!!!!
回答1:
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.
来源:https://stackoverflow.com/questions/58607693/how-to-use-folium-icon-with-fontawesome