Repository navigation
Closed
Description
Describe the bug
When applying a Marker with a CustomIcon to a GeoJson the generate HTML fails in the browser with 'Uncaught TypeError: iconRootAlias.CustomIcon is not a constructor'.
To Reproduce
import folium
m = folium.Map(location=[45.3288, -121.6625], zoom_start=12)
url = "https://leafletjs.com/examples/custom-icons/{}".format
icon_image = url("leaf-red.png")
shadow_image = url("leaf-shadow.png")
icon = folium.CustomIcon(
icon_image,
icon_size=(38, 95),
icon_anchor=(22, 94),
)
icon2 = folium.CustomIcon(
icon_image='https://leafletjs.com/examples/custom-icons/leaf-green.png',
icon_size=(38, 95),
icon_anchor=(22, 94),
popup_anchor=(-3, -76)
)
# this works
folium.Marker(
location=[45.3288, -121.6625], icon=icon, popup="Mt. Hood Meadows",
).add_to(m)
#this fails
rt = folium.GeoJson(
"b.geojson",
marker=folium.Marker(icon=icon2)
).add_to(m)
with open("test.html","w") as f:
f.write(m.get_root().render())
================
b.geojson:
[ {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-121.6625,
45.3288
]
}
}
]
}]
Expected behavior
a green Icon overlay a red Icon on the map and no JS errors
Environment (please complete the following information):
- Browser Chrome & Firefoc
- Python version (3.13.3)
- folium version (0.19.6`
- branca version (0.8.1)
Additional context
Add any other context about the problem here.
Possible solutions
The following fix it for CustomIcon with Icon, but I'm not sure whether it impacts other Markers (eg. AwesomeIcons) in the GeoJson Macro.
--- old/folium/features.py 2025-05-31 23:56:47.789211663 +0200
+++ new/folium/features.py 2025-05-31 23:55:55.852144161 +0200
@@ -1940,7 +1940,7 @@
popup_anchor: Optional[Tuple[int, int]] = None,
):
super(Icon, self).__init__()
- self._name = "CustomIcon"
+ self._name = "icon"
self.options = remove_empty(
icon_url=image_to_url(icon_image),
icon_size=icon_size,
folium is maintained by volunteers. Can you help making a fix for this issue?
Metadata
Metadata
Assignees
Labels
No labels
Activity
sgrefen commentedon Jun 1, 2025
As a quick workaround until it is resolved:
hansthen commentedon Jun 1, 2025
I managed to reproduce the error.
hansthen commentedon Jun 1, 2025
@sgrefen
Your proposed change does not seem to break any tests so far. Would you care to make a Pull Request for it?
Fix or mitigate python-visualization#2152. CustomIcon doesn't exist (…
sgrefen commentedon Jun 1, 2025
Pushed.
hansthen commentedon Jun 1, 2025
I don't see it yet. Perhaps you pushed it only to your own fork of the code? If so, you need to go to the python-visualization repo. Then select "Pull Requests" and "New Pull Request". You should see a link "compare across forks". When you click that you should be able to select your own repo and branch to make a Pull Request.
hansthen commentedon Jun 11, 2025
Closed by sgrefen@15d4593