Skip to content

CustomIcon Marker fails when used in GeoJson #2152

Open
@sgrefen

Description

@sgrefen

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions