Skip to content

Commit 51f0e39

Browse files
committed
Merge branch 'clicked-point2' of https://github.com/ATL2001/lonboard into clicked-point2
2 parents 2987f00 + bf26763 commit 51f0e39

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

examples/clicked-point.ipynb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "e328a21f-5254-42ee-9683-136561d96886",
6+
"metadata": {},
7+
"source": [
8+
"# Clicked point\n",
9+
"\n",
10+
"This notebook demonstrates how you can access the coordinate of the most recent click on a Lonboard map and update a set of widgets to display the x and y coordinates using the [`ipywidgets.observe`](https://ipywidgets.readthedocs.io/en/8.1.5/examples/Widget%20Events.html#traitlet-events) method."
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": null,
16+
"id": "f986a64e-269f-4966-93b0-cfa5bf1ba882",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"import ipywidgets as widgets\n",
21+
"import traitlets\n",
22+
"\n",
23+
"from lonboard import Map\n",
24+
"\n",
25+
"## Create a Lonboard map and two widgets to display the coordinate\n",
26+
"m = Map(layers=[])\n",
27+
"clicked_x_display = widgets.FloatText(description=\"last clicked x:\")\n",
28+
"clicked_y_display = widgets.FloatText(description=\"last clicked y:\")\n",
29+
"\n",
30+
"## create a function that accepts the event which fires when we single click in\n",
31+
"## the map while we are not in bounding box selection mode\n",
32+
"def on_map_click(event: traitlets.utils.bunch.Bunch) -> None:\n",
33+
" x,y = event.get(\"new\")\n",
34+
" clicked_x_display.value = x\n",
35+
" clicked_y_display.value = y\n",
36+
"m.observe(on_map_click, \"clicked_point\")\n",
37+
"\n",
38+
"## show the widgets\n",
39+
"widgets.VBox([\n",
40+
" m,\n",
41+
" clicked_x_display,\n",
42+
" clicked_y_display\n",
43+
"])"
44+
]
45+
}
46+
],
47+
"metadata": {
48+
"kernelspec": {
49+
"display_name": "lonboard",
50+
"language": "python",
51+
"name": "lonboard"
52+
},
53+
"language_info": {
54+
"codemirror_mode": {
55+
"name": "ipython",
56+
"version": 3
57+
},
58+
"file_extension": ".py",
59+
"mimetype": "text/x-python",
60+
"name": "python",
61+
"nbconvert_exporter": "python",
62+
"pygments_lexer": "ipython3",
63+
"version": "3.11.4"
64+
}
65+
},
66+
"nbformat": 4,
67+
"nbformat_minor": 5
68+
}

0 commit comments

Comments
 (0)