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">
<head>
    <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.0">
    <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/trackasia-gl.css" />
    <script src="https://unpkg.com/[email protected]/dist/trackasia-gl.js"></script>
    <style>
        body { margin: 0; padding: 0; }
        html, body, #map { height: 100%; }
        #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;
        }
    </style>
</head>
<body>

<div id="map"></div>
<pre id="info"></pre>
<script>
    const map = new trackasiagl.Map({
        container: 'map', // container id
        style: 'https://maps.track-asia.com/styles/v1/streets.json?key=public_key',
        center: {"lat":10.762622,"lng":106.660172}, // 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
            `${JSON.stringify(e.point)
            }<br />${
            // e.lngLat is the longitude, latitude geographical position of the event
            JSON.stringify(e.lngLat.wrap())}`;
    });
</script>

</body>
</html>