Get Coordinates of the Mouse Pointer
Show the geographic coordinates under the mouse pointer as it moves.
const API_KEY = 'toursprung';
const map = new maptoolkit.Map({
container: 'map',
style: `https://static.maptoolkit.net/styles/toursprung/terrain.json?api_key=${API_KEY}`,
center: [11.39085, 47.27574],
zoom: 12,
attributionControl: { compact: false }
});
map.addControl(new maptoolkit.NavigationControl(), 'top-right');
map.on('mousemove', (e) => {
document.getElementById('coords').textContent =
`Lng: ${e.lngLat.lng.toFixed(6)} Lat: ${e.lngLat.lat.toFixed(6)}`;
});<!DOCTYPE html>
<html lang="en">
<head>
<title>Get Coordinates of the Mouse Pointer – Maptoolkit Maps JS</title>
<meta property="og:description" content="Show the geographic coordinates under the mouse pointer as it moves." />
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://unpkg.com/@maptoolkit/maps@11.0.0-beta.2/dist/maptoolkit.js"></script>
<link rel="stylesheet" href="https://unpkg.com/@maptoolkit/maps@11.0.0-beta.2/dist/maptoolkit.css" />
<style>
html, body { width: 100%; height: 100%; margin: 0; padding: 0; }
#map { width: 100%; height: 100%; }
#coords {
position: absolute;
bottom: 30px;
left: 10px;
background: white;
padding: 8px 14px;
border-radius: .4rem;
font-family: monospace;
font-size: 13px;
}
</style>
</head>
<body>
<div id="map"></div>
<div id="coords">Move the mouse over the map</div>
<script>
const API_KEY = 'toursprung';
const map = new maptoolkit.Map({
container: 'map',
style: `https://static.maptoolkit.net/styles/toursprung/terrain.json?api_key=${API_KEY}`,
center: [11.39085, 47.27574],
zoom: 12,
attributionControl: { compact: false }
});
map.addControl(new maptoolkit.NavigationControl(), 'top-right');
map.on('mousemove', (e) => {
document.getElementById('coords').textContent =
`Lng: ${e.lngLat.lng.toFixed(6)} Lat: ${e.lngLat.lat.toFixed(6)}`;
});
</script>
</body>
</html>