python-visualization / folium

Python Data. Leaflet.js Maps.
https://python-visualization.github.io/folium/
MIT License
6.91k stars 2.22k forks source link

Adding multiple Custom Icon in Folium #744

Open shubham039 opened 7 years ago

shubham039 commented 7 years ago

I am creating a Custom Icon in folium, and displaying it on a map, but when i try to add another coordinate with the same Icon, it fails.

import folium
map_osm = folium.Map(location=[45.3288, -121.6625])
icon_url = 'https://cdn1.iconfinder.com/data/icons/maps-locations-2/96/Geo2-Number-512.png'
icon = folium.features.CustomIcon(icon_url,icon_size=(28, 30))  # Creating a custom Icon
folium.Marker(location=[45.3288, -121.6625],icon=icon).add_to(map_osm)  #adding it to the map
map_osm

image

Now adding another location with the same icon

folium.Marker(location=[46.3288, -122.6625],icon=icon).add_to(map_osm)
map_osm

image

[The map_osm points to new location with its default icon, and the previous pin is removed.]

Expected Output should have been two map pins with the same icon.

folium.__version__ = '0.5.0'

shubham039 commented 7 years ago

I got this solved, it needs icon to be initialised everytime you add a marker.

icon = folium.features.CustomIcon(icon_url,icon_size=(28, 30))
folium.Marker(location=[46.3288, -122.6625],icon=icon).add_to(map_osm)
map_osm
ninabel commented 6 years ago

When icon have to be initialised everytime we got huge file. It's a bug.

Conengmo commented 6 years ago

I guess you're right @ninabel, do you want to look into this and open a PR? I'm thinking now we should let an element have multiple parents in a way, but render it only once. Or something.

hbarovertwo commented 3 years ago

Was this ever addressed in another issue? Because I notice the bug mentioned above by @ninabel still persists.

hbshrestha commented 2 years ago

The bug is still persisting. It has not been solved yet. @hbarovertwo

lowdowner commented 2 years ago

I managed to solved this problem by initiliasing the custom icon within a for loop. Example below:

df = {'Lat': [22.50, 63.21, -13.21, 33.46],
                'Lon': [43.91, -22.22, 77.11, 22.11],
                'Color': ['red', 'yellow', 'orange', 'blue']
              }

data = pd.DataFrame(df)

   world = folium.Map(
    zoom_start=2
    )

    x = data[['Lat', 'Lon', 'Color']].copy()

    for index, row in x.iterrows():
         pushpin = folium.features.CustomIcon('/content/drive/My Drive/Colab Notebooks/pushpin.png', icon_size=(30,30))
         folium.Marker([row['Lat'], row['Lon']],
                                    icon=pushpin,
                                    popup=row['Color'],
                                   ).add_to(world)

    world
Screenshot 2022-10-26 at 20 02 30
MiusR commented 6 months ago

Is this issue fix or is there a good work-around that does not load redundant copies of the same image, thus slowing the server?