Skip to content

Commit 244e5f4

Browse files
committed
changes onclick
1 parent 052c71f commit 244e5f4

File tree

2 files changed

+61
-46
lines changed

2 files changed

+61
-46
lines changed

examples/RasterLayer.ipynb

+39-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,20 @@
1313
"metadata": {},
1414
"outputs": [],
1515
"source": [
16-
"from ipyopenlayers import Map, RasterTileLayer"
16+
"from ipyopenlayers import Map, RasterTileLayer\n",
17+
"import configparser\n"
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": 7,
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"import configparser\n",
27+
"config = configparser.ConfigParser()\n",
28+
"config.read('../.env.ini')\n",
29+
"api_key = config['DEFAULT']['api_key']"
1730
]
1831
},
1932
{
@@ -26,7 +39,7 @@
2639
{
2740
"data": {
2841
"application/vnd.jupyter.widget-view+json": {
29-
"model_id": "b55d411654314ab094b4b12ea0055e9f",
42+
"model_id": "aef7ad2f21ec4cb9b7c3207b7158e5ea",
3043
"version_major": 2,
3144
"version_minor": 0
3245
},
@@ -48,14 +61,33 @@
4861
},
4962
{
5063
"cell_type": "code",
51-
"execution_count": 5,
64+
"execution_count": 3,
5265
"metadata": {},
5366
"outputs": [],
5467
"source": [
55-
"import configparser\n",
56-
"config = configparser.ConfigParser()\n",
57-
"config.read('../.env.ini')\n",
58-
"api_key = config['DEFAULT']['api_key']"
68+
"import requests\n",
69+
"import unicodedata\n",
70+
"\n",
71+
"def get_country_from_coordinates_geoapify(lat, lon):\n",
72+
" config = configparser.ConfigParser()\n",
73+
" config.read('../.env.ini')\n",
74+
" api_key = config['DEFAULT']['api_key'] \n",
75+
" url = f\"https://api.geoapify.com/v1/geocode/reverse?lat={lat}&lon={lon}&apiKey={api_key}\"\n",
76+
" response = requests.get(url)\n",
77+
" data = response.json()\n",
78+
" features = data.get('features', [])\n",
79+
"\n",
80+
" if features:\n",
81+
" first_feature = features[0]\n",
82+
" properties = first_feature.get('properties', {})\n",
83+
" country = properties.get('country', None)\n",
84+
" normalized_name = country.split(' ')[0]\n",
85+
" normalized_name = unicodedata.normalize('NFKD', normalized_name)\n",
86+
" normalized_name = normalized_name.encode('ASCII', 'ignore').decode('utf-8')\n",
87+
"\n",
88+
" print(normalized_name)\n",
89+
"\n",
90+
"m.on_click(get_country_from_coordinates_geoapify)\n"
5991
]
6092
},
6193
{

ipyopenlayers/Map.py

+22-39
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class Map(DOMWidget):
144144

145145
def __init__(self, center=None, zoom=None, **kwargs):
146146
super().__init__(**kwargs)
147-
147+
self._click_callbacks = []
148148
if center is not None:
149149
self.center = center
150150
if zoom is not None:
@@ -183,60 +183,43 @@ def _handle_msg(self, msg):
183183
buffers = msg.get('buffers', [])
184184
self._msg_callback(self, content, buffers)
185185

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."""
194188

195-
callbacks = self._click_callbacks.get_callbacks() if self._click_callbacks else []
189+
190+
callbacks = self._click_callbacks.get_callbacks()
196191
for callback in callbacks:
197192
if callable(callback):
198-
callback(lat, lon, country)
193+
callback(lat, lon)
199194

200-
def on_click(self, callback, api_key):
195+
def on_click(self, callback):
201196
"""
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.
203204
"""
205+
204206
self._click_callback = callback
205-
207+
208+
206209
def handle_frontend_event(widget, content, buffers):
207210
"""Handle the click event from the frontend."""
208211
data = content.get('data', {})
209212
method = data.get('method', '')
210213

214+
211215
if method == 'custom':
212216
event_data = data.get('content', {})
213217
lon = event_data.get('lon')
214218
lat = event_data.get('lat')
215-
219+
216220
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)
218222

219223
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

Comments
 (0)