Folium Search Plugin No Results for FeatureGroup

2024/10/10 16:19:03

I'm trying to add search functionality to a map I'm generating in Python with Folium. I see there is a handy Search plugin available and able to implement it successfully and get it added to the map. Unfortunately, using a FeatureGroup as the layer my FeatureGroup filled with markers can't seem to get the search to bring back results.

My assumption is that the search function would query the tooltip and/or popup attributes of the markers to return the lat/lon as the search value. I've tried manually supplying the value 'tooltip' to the search_label option of the Search function, but no luck.

import pandas as pd
import folium
from folium.plugins import Searchdef mapGenerator(data):map = folium.Map()fg = folium.FeatureGroup()for index, row in data.iterrows():marker = folium.Marker(location=[row['lat'], row['lon']],popup=row['name'])marker.add_to(fg)fg.add_to(map)Search(fg).add_to(map)map.save('map.html')data = pd.DataFrame({'name': ['first', 'second', 'third'], 'lat': [28.27724, 48.52228, 22.43949],'lon':[-9.72904, 34.77667, 102.49105]})mapGenerator(data)

This code produces a map that plots some random points and then adds a search box. My expected and the desired result is that the search bar will zoom in on the coordinate with the name 'first' if I search 'first', or even if I searched 'fir' (or some variation), but currently, no results are found no matter what.

example output

Answer
import pandas as pd
import folium
from folium.plugins import Search
import geopandas as gpddef mapGenerator(data):# Create geometry from the coordinatesdata['geometry'] = data.apply(lambda x: Point(x.lon,x.lat),axis=1)# Convert to geopandas dataframegdf = gpd.GeoDataFrame(data, crs="EPSG:4326")m = folium.Map()# Create an individual FeatureGroup specific for the search. Set show=False to hide the points.fg_search = folium.FeatureGroup(name='search', show=False)m.add_child(fg_search)search_input = fg_search.add_child(folium.GeoJson(gdf))searchbar = Search(layer=search_input,geom_type="Point",placeholder="Search name",collapsed=False,search_label="name",weight=1,).add_to(m)# Create all other FeatureGroups you would like tofg = folium.FeatureGroup(name='FeatureGroup')for index, row in data.iterrows():marker = folium.Marker(location=[row['lat'], row['lon']],popup=row['name'])marker.add_to(fg)fg.add_to(m)# Add layer controlm.add_child(folium.LayerControl())m.save('map.html')data = pd.DataFrame({'name': ['first', 'second', 'third'], 'lat': [28.27724, 48.52228, 22.43949],'lon':[-9.72904, 34.77667, 102.49105]})mapGenerator(data)
https://en.xdnf.cn/q/69878.html

Related Q&A

writing dictionary of dictionaries to .csv file in a particular format

I am generating a dictionary out of multiple .csv files and it looks like this (example):dtDict = {AV-IM-1-13991730: {6/1/2014 0:10: 0.96,6/1/2014 0:15: 0.92,6/1/2014 0:20: 0.97},AV-IM-1-13991731: {6/1…

How to import SSL certificates for Firefox with Selenium [in Python]?

Trying to find a way to install a particular SSL certificate in Firefox with Selenium, using the Python WebDriver and FirefoxProfile. We need to use our own, custom certificate which is stored in the …

Cell assignment of a 2-dimensional Matrix in Python, without numpy

Below is my script, which basically creates a zero matrix of 12x8 filled with 0. Then I want to fill it in, one by one. So lets say column 2 row 0 needs to be 5. How do I do that? The example below sh…

Fill matplotlib subplots by column, not row

By default, matplotlib subplots are filled by row, not by column. To clarify, the commandsplt.subplot(nrows=3, ncols=2, idx=2) plt.subplot(nrows=3, ncols=2, idx=3)first plot into the upper right plot o…

Find the 2nd highest element

In a given array how to find the 2nd, 3rd, 4th, or 5th values? Also if we use themax() function in python what is the order of complexity i.e, associated with this function max()?.def nth_largest(…

pandas data frame - select rows and clear memory?

I have a large pandas dataframe (size = 3 GB):x = read.table(big_table.txt, sep=\t, header=0, index_col=0)Because Im working under memory constraints, I subset the dataframe:rows = calculate_rows() # a…

How do I format a websocket request?

Im trying to create an application in Python that powers a GPIO port when the balance of a Dogecoin address changes. Im using the websocket API here and this websocket client.My code looks like this:fr…

cherrypy and wxpython

Im trying to make a cherrypy application with a wxpython ui. The problem is both libraries use closed loop event handlers. Is there a way for this to work? If I have the wx ui start cherrypy is that g…

What is the logic behind d3.js nice() ticks

I have generated some charts in d3.js. I use the following code to calculate the values to put in my y axis which works like a charm.var s = d3.scale.linear().domain([minValue, maxValue]); var ticks = …

Changing iterable variable during loop

Let it be an iterable element in python. In what cases is a change of it inside a loop over it reflected? Or more straightforward: When does something like this work?it = range(6) for i in it:it.remo…