diff --git a/examples/clicked-point.ipynb b/examples/clicked-point.ipynb new file mode 100644 index 00000000..2614e6e5 --- /dev/null +++ b/examples/clicked-point.ipynb @@ -0,0 +1,68 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "e328a21f-5254-42ee-9683-136561d96886", + "metadata": {}, + "source": [ + "# Clicked point\n", + "\n", + "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." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f986a64e-269f-4966-93b0-cfa5bf1ba882", + "metadata": {}, + "outputs": [], + "source": [ + "import ipywidgets as widgets\n", + "import traitlets\n", + "\n", + "from lonboard import Map\n", + "\n", + "## Create a Lonboard map and two widgets to display the coordinate\n", + "m = Map(layers=[])\n", + "clicked_x_display = widgets.FloatText(description=\"last clicked x:\")\n", + "clicked_y_display = widgets.FloatText(description=\"last clicked y:\")\n", + "\n", + "## create a function that accepts the event which fires when we single click in\n", + "## the map while we are not in bounding box selection mode\n", + "def on_map_click(event: traitlets.utils.bunch.Bunch) -> None:\n", + " x,y = event.get(\"new\")\n", + " clicked_x_display.value = x\n", + " clicked_y_display.value = y\n", + "m.observe(on_map_click, \"clicked_point\")\n", + "\n", + "## show the widgets\n", + "widgets.VBox([\n", + " m,\n", + " clicked_x_display,\n", + " clicked_y_display\n", + "])" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "lonboard", + "language": "python", + "name": "lonboard" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}