Skip to content

Repeated CustomIcon wipes information #2039

Closed
@dkrasne

Description

@dkrasne

Describe the bug

When using FeatureGroupSubGroup and setting the marker to a CustomIcon, the layer isn't generated; it also wipes any customization from other markers added prior to the FeatureGroupSubGroup, and any added afterwards don't appear either. HOWEVER, the problem actually lies with CustomIcon, rather than FeatureGroupSubGroup, because there's also an issue if I simply try to use the same CustomIcon for multiple markers.

To Reproduce

import pandas as pd
import folium
import numpy as np
from folium.plugins import MarkerCluster
from folium.plugins import FeatureGroupSubGroup

map_df = pd.DataFrame(
    columns=["Collection Type", "Institution Name", "Collection Name", "address", "latitude", "longitude"],
    data=[["Academic Library", "Amherst College", "Robert Frost Library", "61 Quadrangle Dr, Amherst, MA 01002, USA", 42.371830, -72.516988],
          ["Other Library/Archive", "Wendy's Subway", np.nan, "379 Bushwick Ave, Brooklyn, NY 11206, USA", 40.703840, -73.937981]]
)

zoom_start=3
start_loc = [map_df.latitude.mean(), map_df.longitude.mean()]

m = folium.Map(location=start_loc, 
               zoom_start=zoom_start,
               control_scale=True)

icon_url = 'https://pratt.darcykrasne.com/cba_bookarts_map/{}'.format
icon_image = icon_url('cba_mapicon.png')
icon_shadow_image = icon_url('cba_mapicon_shadow.png')
cba_icon = folium.CustomIcon(icon_image=icon_image,
                            icon_size=(20,20),
                            icon_anchor=(10,20),
                            shadow_image=icon_shadow_image,
                            shadow_size=(30,7),
                            shadow_anchor=(4,7)
                            )

folium.Marker(location=start_loc,
              icon=cba_icon, popup=folium.Popup("#1")).add_to(m) # If I were to generate the map at this point, everything would be okay. After this, anything added is problematic.

folium.Marker(location=[start_loc[0]-20,start_loc[1]-20],
              icon=cba_icon, 
              popup=folium.Popup("#2")).add_to(m) # If I generate the map at this point, there's already a problem: the second marker doesn't appear, and the popup for the first is empty.

mcluster = MarkerCluster(name="Collections", control=False)
m.add_child(mcluster)

for collection_type in map_df['Collection Type'].unique():
    subgroup = FeatureGroupSubGroup(mcluster, f'{collection_type}')
    m.add_child(subgroup)
    for idx, row in map_df.query(f'`Collection Type` == "{collection_type}"').iterrows():
        collection_type = row["Collection Type"]
        coordinates = [row["latitude"], row["longitude"]]
        popup_text = []
        for item in [row['Collection Name'],row['Institution Name'],row['address']]:
            if item is not np.nan:
                popup_text.append(item)
        popup_html = ""
        for item in popup_text:
            popup_html += item+'<br>'
        marker = folium.Marker(location=coordinates,
                               icon=cba_icon,
                               popup=folium.Popup(popup_html, min_width=100, max_width=500))
        marker.add_to(subgroup)

folium.LayerControl(collapsed=False).add_to(m)

folium.Marker(location=[start_loc[0]-40,start_loc[1]-40],
              icon=cba_icon, popup=folium.Popup(popup_html="#3")).add_to(m) # Also doesn't appear; I just added this for further demonstration.

m

Expected behavior

The customized markers should all be present on the map, with their popup information intact.

Environment (please complete the following information):

  • Browser N/A - running in VS Code
  • Jupyter Notebook
  • Python version: sys.version_info(major=3, minor=11, micro=0, releaselevel='final', serial=0)
  • folium version: 0.15.0
  • branca version: 0.7.0

Additional context

I think I've described everything above; let me know if you need more information.

Possible solutions

Workaround: Generate a new CustomIcon for each marker instead of reusing the same one (I tested it, and it works if you do this within the loop).

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