@@ -144,7 +144,7 @@ class Map(DOMWidget):
144
144
145
145
def __init__ (self , center = None , zoom = None , ** kwargs ):
146
146
super ().__init__ (** kwargs )
147
-
147
+ self . _click_callbacks = []
148
148
if center is not None :
149
149
self .center = center
150
150
if zoom is not None :
@@ -183,60 +183,43 @@ def _handle_msg(self, msg):
183
183
buffers = msg .get ('buffers' , [])
184
184
self ._msg_callback (self , content , buffers )
185
185
186
- def _handle_click (self , lat , lon , api_key ):
187
- """
188
- Handle click events and trigger registered callbacks.
189
- """
190
- print (f"Handling click event at Longitude: { lon } , Latitude: { lat } " )
191
- country = self .get_country_from_coordinates_geoapify (lat , lon , api_key )
192
- self .clicked_country = country
193
- print (f"Country: { country } " )
186
+ def _handle_click (self , lat , lon ):
187
+ """Handle click events and trigger registered callbacks."""
194
188
195
- callbacks = self ._click_callbacks .get_callbacks () if self ._click_callbacks else []
189
+
190
+ callbacks = self ._click_callbacks .get_callbacks ()
196
191
for callback in callbacks :
197
192
if callable (callback ):
198
- callback (lat , lon , country )
193
+ callback (lat , lon )
199
194
200
- def on_click (self , callback , api_key ):
195
+ def on_click (self , callback ):
201
196
"""
202
- Register a callback to handle click events on the map and pass the API key.
197
+ Register a callback to handle click events on the map.
198
+
199
+
200
+ Parameters
201
+ ----------
202
+ callback : function
203
+ Function that accepts two arguments: lon (longitude) and lat (latitude) of the clicked position.
203
204
"""
205
+
204
206
self ._click_callback = callback
205
-
207
+
208
+
206
209
def handle_frontend_event (widget , content , buffers ):
207
210
"""Handle the click event from the frontend."""
208
211
data = content .get ('data' , {})
209
212
method = data .get ('method' , '' )
210
213
214
+
211
215
if method == 'custom' :
212
216
event_data = data .get ('content' , {})
213
217
lon = event_data .get ('lon' )
214
218
lat = event_data .get ('lat' )
215
-
219
+
216
220
if callable (self ._click_callback ):
217
- self ._click_callback (lat , lon , self . get_country_from_coordinates_geoapify ( lat , lon , api_key ) )
221
+ self ._click_callback (lat , lon )
218
222
219
223
self .on_msg (handle_frontend_event )
220
-
221
- def normalize_country_name (self , country_name ):
222
- normalized_name = country_name .split (' ' )[0 ]
223
- normalized_name = unicodedata .normalize ('NFKD' , normalized_name )
224
- normalized_name = normalized_name .encode ('ASCII' , 'ignore' ).decode ('utf-8' )
225
- return normalized_name
226
-
227
- def get_country_from_coordinates_geoapify (self , lat , lon , api_key ):
228
- url = f"https://api.geoapify.com/v1/geocode/reverse?lat={ lat } &lon={ lon } &apiKey={ api_key } "
229
- response = requests .get (url )
230
- data = response .json ()
231
- features = data .get ('features' , [])
232
-
233
- if features :
234
- first_feature = features [0 ]
235
- properties = first_feature .get ('properties' , {})
236
- country = properties .get ('country' , None )
237
- if country :
238
- normalized_name = country .split (' ' )[0 ]
239
- normalized_name = unicodedata .normalize ('NFKD' , normalized_name )
240
- normalized_name = normalized_name .encode ('ASCII' , 'ignore' ).decode ('utf-8' )
241
- return normalized_name
242
- return "Unknown"
224
+
225
+
0 commit comments