My folium map, marker cluster, layer control, and search bar all work and show up correctly except for actually using the search bar. The layer I have the search plugin point to when searching is my MarkerCluster layer, which the folium documentation says should be searchable. Anything I type into the search bar it returns "Location not found" which makes me think it's not searching through the MarkerCluster layer correctly, or does not know to search through the text of that layer that is included in the markers' popups.
Here is my code as I have it right now:
import os
import folium
from folium import plugins
import pandas as pd
from folium.plugins import MarkerCluster
from folium.plugins import Search
#import data including lat/lon, name of service, address of service
df = pd.read_csv('data.csv')
#Create base map zoomed in to Indiana
map=folium.Map(location=[39.80, -86.12], tiles=None, zoom_start=7)
folium.TileLayer('cartodbpositron', name='COVID-19 Services').add_to(map)
#Make Marker Cluster Group layer
mcg = folium.plugins.MarkerCluster(control=False)
map.add_child(mcg)
#Create layer of markers
#Set marker popups to display name and address of service
for row in df.iterrows():
row_values=row[1]
location=[row_values['latitude'], row_values['longitude']]
popup=popup=(row_values['name']+'<br>'+'<br>'+row_values['address_1']+
'<br>'+'<br>'+row_values['city']+','+' '+row_values['state']+
'<br>'+row_values['zip'])
marker=folium.Marker(location=location, popup=popup, min_width=2000)
marker.add_to(mcg)
#Add search bar
servicesearch = Search(
layer=mcg,
geom_type='Point',
placeholder='Search for a service',
collapsed=False,
).add_to(map)
#Add layer control
folium.LayerControl().add_to(map)
map
How do I get the search plugin to actually search the test of the marker popups? Then, how do I get the map to highlight or zoom to those searched for markers? Any help is GREATLY appreciated, thank you!
This is my solution:
def visualize_locations_with_marker_cluster(df, zoom=4):
f = folium.Figure(width=1000, height=500)
center_lat=34.686567
center_lon=135.52000
m = folium.Map([center_lat,center_lon], zoom_start=zoom).add_to(f)
marker_cluster = MarkerCluster().add_to(m)
for d in df.iterrows():
folium.Marker(location=[d[1]["y"], d[1]["x"]], popup=d[1]["company"], name=d[1]["company"]).add_to(marker_cluster)
servicesearch = Search(
layer=marker_cluster,
search_label="name",
placeholder='Search for a service',
collapsed=False,
).add_to(m)
return m
First create map, create cluster, loop values in pd.dataframe and create Marekers for Cluster with name label.
Next create Search object and add cluster there with search_label="name", label. Finally add it all back to the map
["x", "y"] is longtitude and latitude, company is a search value in my case
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With