mouse-position
Get mouse position
Add a Style for mouse hover
/* Add Styles */
#mouse-info {
box-sizing: border-box;
margin: 0;
padding: 8px;
position: absolute;
bottom: 24px;
left: 8px;
z-index: 200;
background: gray;
}
Add mouse position info section
<!-- Add mouse position info section. -->
<div id="mouse-info">
<h3>Mouse Hover Position</h3>
<div id="mouse-hover-pos"></div>
<h3>Mouse Click Position</h3>
<div id="mouse-click-pos"></div>
</div>
Add `mousemove` event to get mouse position on hover
// Add `mousemove` event to get mouse position on hover.
map.on("mousemove", (e) => {
document.getElementById(
"mouse-hover-pos"
).innerHTML = `Location: ${e.lngLat}`;
});
Add `click` event to get mouse position on click.
// Add `click` event to get mouse position on click.
map.on("click", (e) => {
document.getElementById(
"mouse-click-pos"
).innerHTML = `Location: ${e.lngLat}`;
});
Full Source Code:
<!DOCTYPE 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" />
<link
rel="stylesheet"
href="https://cdn.barikoi.com/bkoi-gl-js/dist/bkoi-gl.css"
/>
<script src="https://cdn.barikoi.com/bkoi-gl-js/dist/bkoi-gl.js"></script>
<style>
body,
#map {
box-sizing: border-box;
margin: 0;
padding: 0;
width: 100vw;
height: 100vh;
overflow: hidden;
}
/* Add Styles */
#mouse-info {
box-sizing: border-box;
margin: 0;
padding: 8px;
position: absolute;
bottom: 24px;
left: 8px;
z-index: 200;
background: gray;
}
</style>
<title>Get Mouse Position</title>
</head>
<body>
<div id="map"></div>
<!-- Add mouse position info section. -->
<div id="mouse-info">
<h3>Mouse Hover Position</h3>
<div id="mouse-hover-pos"></div>
<h3>Mouse Click Position</h3>
<div id="mouse-click-pos"></div>
</div>
<script>
bkoigl.accessToken = "<Barikoi API Key>"; // required
const map = new bkoigl.Map({
container: "map",
center: [90.3938010872331, 23.821600277500405],
zoom: 12,
});
map.on("load", () => {
// Add `mousemove` event to get mouse position on hover.
map.on("mousemove", (e) => {
document.getElementById(
"mouse-hover-pos"
).innerHTML = ``;
});
// Add `click` event to get mouse position on click.
map.on("click", (e) => {
document.getElementById(
"mouse-click-pos"
).innerHTML = ``;
});
});
</script>
</body>
</html>