问题
I am mapping markers that have a row called "marker_color" indicating "red", "yellow", and "green" based on other column values. How can I add a filter option to the corner of my map that will allow me to only show one, two, all, or none of the markers based on color? Basically, three clickable radio options to render the three colored markers.
Currently, I am mapping all markers like so from my sales_colored dataframe:
basemap2 = generateBaseMap()
for index, row in sales_colored.iterrows():
folium.Circle([row['Latitude'], row['Longitude']],
radius=200, color=row['marker_color'], fill_color=row['marker_color']).add_to(basemap2)
basemap2
回答1:
If you want to add checkbox for each color, you can use a folium.FeatureGroup()
. You need first to collect all unique value in the column sales_colored["marker_color"]
and you create one FeatureGroup for each color (I use a dictionnary called features
to store the colors). You can create a code like this :
features = {}
for row in pd.unique(sales_colored["marker_color"]):
features[row] = folium.FeatureGroup(name=row)
for index, row in sales_colored.iterrows():
circ = folium.Circle([row['Latitude'], row['Longitude']],
radius=200, color=row['marker_color'], fill_color=row['marker_color'])
circ.add_to(features[row['marker_color']])
for row in pd.unique(sales_colored["marker_color"]):
features[row].add_to(basemap2)
folium.LayerControl().add_to(basemap2)
basemap2
来源:https://stackoverflow.com/questions/64435653/filter-folium-map-based-on-marker-color