Skip to content

Commit 78c5455

Browse files
committed
Add location_found event to SearchControl
1 parent 432038c commit 78c5455

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

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)