Skip to content

Get coordinates of the mouse pointer

Show mouse position on hover with pixel and latitude and longitude coordinates.

<!DOCTYPE html>
<html lang="en">
    <title>Get coordinates of the mouse pointer</title>
    <meta property="og:description" content="Show mouse position on hover with pixel and latitude and longitude coordinates." />
    <meta charset='utf-8'>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel='stylesheet' href='[email protected]/dist/maplibre-gl.css' />
    <script src='[email protected]/dist/maplibre-gl.js'></script>
        body { margin: 0; padding: 0; }
        html, body, #map { height: 100%; }
<style type="text/css">
    #info {
        display: block;
        position: absolute;
        top: 20px;
        left: 50%;
        transform: translate(-50%);
        width: 50%;
        padding: 10px;
        border: none;
        border-radius: 3px;
        font-size: 12px;
        text-align: center;
        color: #222;
        background: #fff;
<div id="map"></div>
<pre id="info"></pre>
    const map = new maplibregl.Map({
        container: 'map', // container id
        center: [-74.5, 40], // starting position
        zoom: 9 // starting zoom

    map.on('mousemove', (e) => {
        document.getElementById('info').innerHTML =
            // e.point is the x, y coordinates of the mousemove event relative
            // to the top-left corner of the map
            }<br />${
                // e.lngLat is the longitude, latitude geographical position of the event