Skip to content

Keep Search layer hidden when show=False - #2378

Open
vladosskins178 wants to merge 2 commits into
python-visualization:mainfrom
vladosskins178:fix/search-respect-layer-show
Open

vladosskins178 wants to merge 2 commits into
python-visualization:mainfrom
vladosskins178:fix/search-respect-layer-show

Conversation

@vladosskins178

Copy link
Copy Markdown

Fixes #2377

L.Control.Search adds its layer to the map when the control is added: onAdd calls setLayer(this._layer), and setLayer does this._layer.addTo(this._map) (leaflet-search 2.9.7 source). So a layer created with show=False became visible as soon as a Search control was attached to it, even though folium itself never added it.

This removes the layer again right after addControl when layer.show is False. Layers with show=True are 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):

hidden layer on map at load shown layer on map at load
main true true
this branch false true

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.

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
@nk-batman21

Copy link
Copy Markdown
Contributor

There’s a related case with a hidden parent. If Search targets a GeoJson inside a FeatureGroup(show=False), the child has its default show=True. On f09a521 the group starts off the map, but its point is visible because Search added the child directly.

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, (map.hasLayer(group), map.hasLayer(layer)) was (false, false) without Search and (false, true) with it at load. A shown parent gave (true, true) as expected.

Could the control preserve the layer’s actual map membership? In the generated JS I saved map.hasLayer(layer) immediately before addControl and called layer.remove() afterwards only if it had originally been absent. The hidden case then started (false, false), toggling the parent on gave (true, true), and toggling it off restored (false, false). This extends the direct-layer fix to grouped overlays.

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.
@vladosskins178

Copy link
Copy Markdown
Author

Thanks, good catch. Done in 1834e34: the template now saves map.hasLayer(layer) before addControl and calls layer.remove() afterwards only if the layer was not on the map. This replaces the show=False template check, so it covers a direct hidden layer and a child of a hidden FeatureGroup.

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:

on map at load f09a521 1834e34
hidden layer false false
child of hidden group true false
shown layer true true
child of shown group true true

Toggling the hidden group on gives (true, true) and toggling it off gives (false, false). With the group on, searching "chi" still lists the child's feature. The unit test now checks the order hasLayer, then addControl, then the conditional remove(). It fails on main and passes on the branch.

@nk-batman21

Copy link
Copy Markdown
Contributor

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 false → true → false for the hidden group. The shown group and the direct hidden/shown GeoJson controls also stayed in sync. This resolves the grouped-overlay issue I reported.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Search Class Ignores "show" Argument.

2 participants