{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Handling hover and click map events\n", "\n", "[![open_in_colab][colab_badge]][colab_notebook_link]\n", "[![open_in_binder][binder_badge]][binder_notebook_link]\n", "\n", "[colab_badge]: https://colab.research.google.com/assets/colab-badge.svg\n", "[colab_notebook_link]: https://colab.research.google.com/github/foursquare/unfolded-sdk-examples/blob/master/notebooks/07%20-%20Eventhandling.ipynb\n", "[binder_badge]: https://mybinder.org/badge_logo.svg\n", "[binder_notebook_link]: https://mybinder.org/v2/gh/foursquare/unfolded-sdk-examples/master?urlpath=lab/tree/notebooks/07%20-%20Eventhandling.ipynb\n", "\n", "Using the [set_map_event_handlers](https://docs.unfolded.ai/map-sdk/api/set-map-event-handlers) function it is possible to define event callbacks for the `on_hover` and `on_click` events. These events can return the data from the layer points or polygons if the user clicks or hovers on them." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dependencies\n", "\n", "This notebook requires the following Python dependencies:\n", "\n", "- `unfolded.map-sdk`: The Unfolded Map SDK\n", "\n", "If running this notebook in Binder, these dependencies should already be installed. If running in Colab, the next cell will install these dependencies." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# If in Colab, install this notebook's required dependencies\n", "import sys\n", "if \"google.colab\" in sys.modules:\n", " !pip install 'unfolded.map_sdk>=1.0'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Imports" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from unfolded.map_sdk import create_map\n", "import ipywidgets as widgets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Handling event callbacks" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "unfolded_map = create_map()\n", "unfolded_map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We define the `on_hover` callback function:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "639fbe8d99334002a720c604c8372134", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Output()" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "output = widgets.Output()\n", "@output.capture(clear_output=True)\n", "def on_hover_output(info):\n", " print('Hover event')\n", " print(info)\n", "output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We define the `on_click` callback function:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "e546c2cae5b44c57aea5d80799685dcb", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Output()" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "output = widgets.Output()\n", "@output.capture(clear_output=True)\n", "def on_click_output(info):\n", " print('Click event')\n", " print(info)\n", "output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we register the defined callback functions. These functions will be called once you hover or click on the points or on the empty part of the map for the corresponding function." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "unfolded_map.set_event_handlers({\n", " 'on_hover': on_hover_output,\n", " 'on_click': on_click_output\n", "})" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "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.8.10" } }, "nbformat": 4, "nbformat_minor": 4 }