How can I set filter by month of folium map(Django project)
I am using folium and I can see in front folium map with markers, I use checkbox, but because of I have two month, I want to add radio button and selecting only one month. I want to have filters by months and also by statuses, but with different labels. I used chatgpt but it doesn't help me. I have also tried many things.
What do you suggest as an alternative?
I tried this but it is not working:
GroupedLayerControl(
groups={'groups1': [fg1, fg2]},
collapsed=False,
).add_to(m)
My code:
def site_location(request):
qs = buffer.objects.filter(
meter="Yes",
ext_operators="No",
).exclude(
# hostid="No"
).values('n', 'name', 'village_city', 'region_2', 'rural_urban', 'hostid',
'latitude', 'longitude', 'site_type', 'meter', 'ext_operators',
'air_cond', 'kw_meter', 'kw_month_noc', 'buffer', 'count_records',
'fix_generator', 'record_date', 'id', 'date_meter'
)
data = list(qs)
if not data:
return render(request, "Energy/siteslocation.html", {"my_map": None})
m = folium.Map(location=[42.285649704648866, 43.82418523761071], zoom_start=8)
critical = folium.FeatureGroup(name="Critical %(100-..)")
warning = folium.FeatureGroup(name="Warning %(70-100)")
moderate = folium.FeatureGroup(name="Moderate %(30-70)")
positive = folium.FeatureGroup(name="Positive %(0-30)")
negative = folium.FeatureGroup(name="Negative %(<0)")
check_noc = folium.FeatureGroup(name="Check_noc")
check_noc_2 = folium.FeatureGroup(name="Check_noc_2")
for row in data:
comments_qs = SiteComment.objects.filter(site_id=row["id"]).order_by('-created_at')[
:5]
if comments_qs.exists():
comments_html = ""
for c in comments_qs:
comments_html += (
f"<br><span style='font-size:12px; color:black'>"
f"{c.ip_address} - {c.created_at.strftime('%Y-%m-%d %H:%M')}: {c.comment}</span>"
)
else:
comments_html = "<br><span style='font-size:13px; color:black'>....</span>"
html = (
f"<a target='_blank' href='http://127.0.0.1:8000/Energy/sitechart/' "
f"style='font-size:14px; font-family:verdana'>{row['name']}</a>"
.....
)
# Determine marker color
if row["hostid"] == 'nan':
icon_color = "lightblue" # for
folium.Marker(
location=[row["latitude"], row["longitude"]],
popup=folium.Popup(html, max_width=350),
icon=folium.Icon(color=icon_color, icon="fa-phone", prefix='fa')
).add_to(check_noc_2)
...........
else:
icon_color = "red" # for critical
folium.Marker(
location=[row["latitude"], row["longitude"]],
popup=folium.Popup(html, max_width=350),
icon=folium.Icon(color=icon_color, icon="fa-phone", prefix='fa')
).add_to(critical)
# Add layers and controls
check_noc_2.add_to(m)
check_noc.add_to(m)
negative.add_to(m)
positive.add_to(m)
moderate.add_to(m)
warning.add_to(m)
critical.add_to(m)
folium.plugins.Fullscreen(
position="topright",
title="Fullscreen",
title_cancel="Exit",
force_separate_button=True,
).add_to(m)
Geocoder(collapsed=True, add_marker=False).add_to(m)
folium.LayerControl(collapsed=False).add_to(m)
# Render map HTML
map_html = m._repr_html_()
return render(request, "Energy/siteslocation.html", {"my_map": map_html})