Skip to content

Commit 66982eb

Browse files
authored
Merge pull request #891 from martinRenou/search_location_found_callback
SearchControl: location found callback
2 parents 432038c + 94368a1 commit 66982eb

File tree

3 files changed

+56
-14
lines changed

3 files changed

+56
-14
lines changed

docs/source/api_reference/search_control.rst

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,27 @@ Example
2121

2222
m
2323

24+
You can add a callback that will be run when on search found:
25+
26+
.. jupyter-execute::
27+
28+
m = Map(center=[47, 2], zoom=5)
29+
30+
search = SearchControl(
31+
position="topleft",
32+
url='https://cartoradon.irsn.fr/commune.py/communes/search/FR/{s}?',
33+
zoom=5
34+
)
35+
m.add_control(search)
36+
37+
def on_found(**kwargs):
38+
# Print the result of the search (text, location etc)
39+
print(kwargs)
40+
41+
search.on_location_found(on_found)
42+
43+
m
44+
2445
You can also search features from GeoJSON layers.
2546

2647
.. jupyter-execute::
@@ -77,5 +98,6 @@ Methods
7798
================ ===================================== ===
7899
Method Arguments Doc
79100
================ ===================================== ===
101+
on_location_found Callable object Adds a callback on location found event.
80102
on_feature_found Callable object Adds a callback on found event for searching in GeoJSON layer.
81-
================ ===================================== ===
103+
================ ===================================== ===

ipyleaflet/leaflet.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1894,15 +1894,19 @@ class SearchControl(Control):
18941894
marker = Instance(Marker, allow_none=True, default_value=None).tag(sync=True, **widget_serialization)
18951895
layer = Instance(LayerGroup, allow_none=True, default_value=None).tag(sync=True, **widget_serialization)
18961896

1897+
_location_found_callbacks = Instance(CallbackDispatcher, ())
18971898
_feature_found_callbacks = Instance(CallbackDispatcher, ())
18981899

18991900
def __init__(self, **kwargs):
19001901
super().__init__(**kwargs)
19011902
self.on_msg(self._handle_leaflet_event)
19021903

19031904
def _handle_leaflet_event(self, _, content, buffers):
1904-
if content.get('event', '') == 'found':
1905-
self._feature_found_callbacks(**content)
1905+
if content.get('event', '') == 'locationfound':
1906+
if content.get('feature', None) is not None:
1907+
self._feature_found_callbacks(**content)
1908+
else:
1909+
self._location_found_callbacks(**content)
19061910

19071911
def on_feature_found(self, callback, remove=False):
19081912
"""Add a found feature event listener for searching in GeoJSON layer.
@@ -1916,6 +1920,18 @@ def on_feature_found(self, callback, remove=False):
19161920
"""
19171921
self._feature_found_callbacks.register_callback(callback, remove=remove)
19181922

1923+
def on_location_found(self, callback, remove=False):
1924+
"""Add a found location event listener. The callback will be called when a search result has been found.
1925+
1926+
Parameters
1927+
----------
1928+
callback : callable
1929+
Callback function that will be called on location found event.
1930+
remove: boolean
1931+
Whether to remove this callback or not. Defaults to False.
1932+
"""
1933+
self._location_found_callbacks.register_callback(callback, remove=remove)
1934+
19191935

19201936
class MapStyle(Style, Widget):
19211937
"""Map Style Widget

js/src/controls/SearchControl.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,21 @@ export class LeafletSearchControlView extends control.LeafletControlView {
5050
}
5151

5252
leaflet_events() {
53-
if (this.model.get('layer') !== null) {
53+
this.obj.on('search:locationfound', (e) => {
54+
if (e.layer !== null) {
5455
var found_style = this.model.get('found_style');
55-
this.obj.on('search:locationfound', (e) => {
56-
e.layer.setStyle(found_style);
57-
if(e.layer._popup)
58-
e.layer.openPopup();
59-
this.send({
60-
event: 'found',
61-
feature: e.layer.feature
62-
});
63-
});
64-
}
56+
e.layer.setStyle(found_style);
57+
if (e.layer._popup) {
58+
e.layer.openPopup();
59+
}
60+
}
61+
62+
this.send({
63+
event: 'locationfound',
64+
text: e.text,
65+
feature: e.layer !== null ? e.layer.feature : null,
66+
location: [e.latlng.lat, e.latlng.lng]
67+
});
68+
});
6569
}
6670
}

0 commit comments

Comments
 (0)