Skip to content Skip to sidebar Skip to footer

Python - Folium Search Plugin Doesn't Search Popup Text In Markercluster Group Layer

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 wh

Solution 1:

If you change a little how you add data to the map, it will be easier to use the search bar. You can change the data in your dataframe into a GeoJSON objet. You first need to create a dictionnary for your GeoJSON and use the function folium.GeoJSON() :

geo_json = {
  "type": "FeatureCollection",
  "features": [],
}
for d in df.iterrows():
    temp_dict = {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates":[d[1]["longitude"], d[1]["latitude"]],
        
      },"properties": {"name": d[1]["name"]}
    }
    geo_json["features"].append(temp_dict)
geojson_obj = folium.GeoJson(geo_json).add_to(map)

After that, you just need to change a little your code to add the search bar :

servicesearch = Search(
    layer=geojson_obj,
    search_label="name",
    placeholder='Search for a service',
    collapsed=False,
).add_to(map)

Solution 2:

I have been struggling a while with the same. I believer this might fix it: add a 'name' or whatever you want to be searched to the marker, e.g.:

marker=folium.Marker(location=location, popup=popup, min_width=2000, name=name)

A little late, but better than never, perhaps.

Solution 3:

This is my solution:

defvisualize_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

Post a Comment for "Python - Folium Search Plugin Doesn't Search Popup Text In Markercluster Group Layer"