---
title: "Get coordinates of the mouse pointer | JavaScript maps SDK"
description: "Show mouse position on hover with coordinates."
---

> For AI agents: a documentation index is available at [/llms.txt](https://docs.maptiler.com/llms.txt). Markdown versions of all pages are available by appending .md to any URL.

# Get coordinates of the mouse pointer

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

### NPM module setup

```bash
npm install --save @maptiler/sdk
```

#### main.js

```js
import { Map, MapStyle, config } from '@maptiler/sdk';
import '@maptiler/sdk/dist/maptiler-sdk.css';

config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
const map = new Map({
    container: 'map', // container id
    style: MapStyle.STREETS, 
    center: [-122.42162, 37.78106], // starting position
    zoom: 12.69 // starting zoom
});

map.on('mousemove', function (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());
});
```

#### index.html

```html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="robots" content="noindex">
  <title>Get coordinates of the mouse pointer</title>
  <link rel="stylesheet" href="./style.css">
</head>
<body>
  <style type="text/css">
  #info {
      display: block;
      position: relative;
      margin: 0px auto;
      width: 50%;
      padding: 10px;
      border: none;
      border-radius: 3px;
      font-size: 12px;
      text-align: center;
      color: #222;
      background: #fff;
  }
  </style>
  <div id="map"></div>
  <pre id="info"></pre>
  <script type="module" src="./main.js"></script>
</body>
</html>
```

#### style.css

```css
body {margin: 0; padding: 0;}
#map {position: absolute; top: 0; bottom: 0; width: 100%;}
```

### Basic JavaScript setup

```html
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Get coordinates of the mouse pointer</title>
<script src="https://cdn.maptiler.com/maptiler-sdk-js/v4.2.0/maptiler-sdk.umd.min.js"></script>
<link href="https://cdn.maptiler.com/maptiler-sdk-js/v4.2.0/maptiler-sdk.css" rel="stylesheet" />
<style>
  body {margin: 0; padding: 0;}
  #map {position: absolute; top: 0; bottom: 0; width: 100%;}
</style>
</head>
<body>
<style type="text/css">
    #info {
        display: block;
        position: relative;
        margin: 0px auto;
        width: 50%;
        padding: 10px;
        border: none;
        border-radius: 3px;
        font-size: 12px;
        text-align: center;
        color: #222;
        background: #fff;
    }
</style>
<div id="map"></div>
<pre id="info"></pre>
<script>
    maptilersdk.config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
    const map = new maptilersdk.Map({
        container: 'map', // container id
        style: maptilersdk.MapStyle.STREETS, 
        center: [-122.42162, 37.78106], // starting position
        zoom: 12.69 // starting zoom
    });

    map.on('mousemove', function (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>
```

## Related examples

### [Show drawn polygon area](https://docs.maptiler.com/sdk-js/examples/mapbox-gl-draw/)

Examples

Use mapbox-gl-draw to draw a polygon and Turf.js to calculate its area in square meters.

### [Measure distances](https://docs.maptiler.com/sdk-js/examples/measure/)

Examples

To calculate distances on the map, simply click on different points to create lines that will measure the distances using turf.length.

### [Get features under the mouse pointer](https://docs.maptiler.com/sdk-js/examples/queryrenderedfeatures/)

Examples

Show properties of hovered map elements with queryRenderedFeatures.
