Skip to content

Mouse position

image image image

Get coordinates of the mouse pointer

This source code of this example is adapted from the MapLibre GL JS example - Get coordinates of the mouse pointer.

Uncomment the following line to install leafmap if needed.

1
# %pip install "leafmap[maplibre]"
1
2
import ipywidgets as widgets
import leafmap.maplibregl as leafmap

To run this notebook, you will need an API key from MapTiler. Once you have the API key, you can uncomment the following code block and replace YOUR_API_KEY with your actual API key. Then, run the code block code to set the API key as an environment variable.

1
2
# import os
# os.environ["MAPTILER_KEY"] = "YOUR_API_KEY"
1
2
m = leafmap.Map(center=[-74.5, 40], zoom=9, style="streets")
m
1
m.clicked
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
output = widgets.Output()


def log_lng_lat(lng_lat):
    with output:
        output.clear_output()
        print(lng_lat.new)


m.observe(log_lng_lat, names="clicked")
output