Skip to content

CustomIcon Marker fails when used in GeoJson #2152

Closed

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?

Activity

  1. sgrefen commented on Jun 1, 2025

    @sgrefen
    ContributorAuthor

    As a quick workaround until it is resolved:

    import folium
    
    v=[int(i) for i in folium.__version__.split('.')]
    if v[0]<1 and v[1]<20 and v[2]<7:
        def helper(**args):
            r=folium.CustomIcon(**args)
            r._name='icon'
            return r
        CustomIcon=helper
    
    else:
        CustomIcon=folium.CustomIcon
    
    icon2 = CustomIcon(
        icon_image='https://leafletjs.com/examples/custom-icons/leaf-green.png',
        icon_size=(38, 95),
        icon_anchor=(22, 94),
        popup_anchor=(-3, -76)
    
  2. hansthen commented on Jun 1, 2025

    @hansthen
    Collaborator

    I managed to reproduce the error.

  3. hansthen commented on Jun 1, 2025

    @hansthen
    Collaborator

    @sgrefen

    Your proposed change does not seem to break any tests so far. Would you care to make a Pull Request for it?

  4. added a commit that references this issue on Jun 1, 2025
  5. sgrefen commented on Jun 1, 2025

    @sgrefen
    ContributorAuthor

    @sgrefen

    Your proposed change does not seem to break any tests so far. Would you care to make a Pull Request for it?

    Pushed.

  6. hansthen commented on Jun 1, 2025

    @hansthen
    Collaborator

    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.

  7. hansthen commented on Jun 11, 2025

    @hansthen
    Collaborator

    Closed by sgrefen@15d4593

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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