Get coordinates of the mouse pointer

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

NPM module setup


npm install --save @maptiler/sdk
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());
});
Show drawn polygon area

Show drawn polygon area

Examples

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

Distance measurement on map

Measure distances

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

Get features under the mouse pointer

Examples

Show properties of hovered map elements with queryRenderedFeatures.

Was this helpful?