Repository navigation
Keep Search layer hidden when show=False - #2378
vladosskins178 wants to merge 2 commits into
Conversation
L.Control.Search adds its layer to the map in onAdd (setLayer calls layer.addTo(map)), so a layer created with show=False became visible as soon as a Search control was attached to it. Remove the layer again right after the control is added when show is False. Fixes python-visualization#2377
|
There’s a related case with a hidden parent. If m = folium.Map([0, 0], tiles=None)
group = folium.FeatureGroup(name="Regions", show=False).add_to(m)
layer = folium.GeoJson({
"type": "FeatureCollection",
"features": [{"type": "Feature", "properties": {"name": "test"},
"geometry": {"type": "Point", "coordinates": [0, 0]}}],
}).add_to(group)
Search(layer, search_label="name").add_to(m)In Chrome with Leaflet 1.9.4 and leaflet-search 2.9.7, Could the control preserve the layer’s actual map membership? In the generated JS I saved |
Check map.hasLayer(layer) before addControl and remove the layer again only if it was not on the map. This also covers a layer inside a hidden FeatureGroup, which keeps its own show=True.
|
Thanks, good catch. Done in 1834e34: the template now saves Checked in headless Chrome with Leaflet 1.9.4 and leaflet-search 2.9.7. The map had a hidden GeoJson, your hidden-group case, a shown GeoJson and a GeoJson in a shown group, each with its own Search:
Toggling the hidden group on gives |
|
Confirmed the hidden-parent case on 1834e34 in Chrome. I clicked the Leaflet LayerControl checkbox twice and checked the checkbox state, the GeoJson’s map membership and the point marker’s membership at load and after each click. All three went |
Fixes #2377
L.Control.Searchadds its layer to the map when the control is added:onAddcallssetLayer(this._layer), andsetLayerdoesthis._layer.addTo(this._map)(leaflet-search 2.9.7 source). So a layer created withshow=Falsebecame visible as soon as aSearchcontrol was attached to it, even though folium itself never added it.This removes the layer again right after
addControlwhenlayer.showis False. Layers withshow=Trueare unchanged.Checked in headless Chrome with the issue's setup (a hidden GeoJson with a Search control, plus a shown GeoJson with its own Search control, and a LayerControl):
After switching the hidden layer on in the LayerControl, the search still finds its features.
Tests: two cases in
tests/plugins/test_search.py(hidden layer is removed after the control is added; shown layer is not removed). The first one fails on main.