获取鼠标指针坐标
获取鼠标指针的坐标;
<!DOCTYPE html>
<html lang="en">
<head>
<title>获取鼠标指针坐标</title>
<meta property="og:description" content="获取鼠标指针的坐标" />
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel='stylesheet' href='https://unpkg.com/maplibre-gl@5.5.0/dist/maplibre-gl.css' />
<script src='https://unpkg.com/maplibre-gl@5.5.0/dist/maplibre-gl.js'></script>
<style>
body { margin: 0; padding: 0; }
html, body, #map { height: 100%; }
.coordinates-box {
position: absolute;
bottom: 20px;
left: 10px;
padding: 10px;
background-color: rgba(255, 255, 255, 0.9);
font-family: 'Arial', sans-serif;
font-size: 14px;
color: #333;
border-radius: 4px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.coordinates-box h3 {
margin: 0 0 10px 0;
font-size: 16px;
border-bottom: 1px solid #ddd;
padding-bottom: 5px;
}
.coordinates-container {
display: grid;
grid-template-columns: auto auto;
grid-gap: 5px;
}
.coordinates-container div {
padding: 5px 0;
}
.label {
font-weight: bold;
padding-right: 10px;
}
.value {
font-family: 'Courier New', monospace;
min-width: 150px;
}
.dms-value {
font-size: 12px;
color: #666;
}
.copy-button {
margin-top: 10px;
padding: 5px 10px;
background-color: #4285F4;
color: white;
border: none;
border-radius: 3px;
cursor: pointer;
font-size: 12px;
}
.copy-button:hover {
background-color: #3367D6;
}
.copy-message {
display: none;
font-size: 12px;
color: #4CAF50;
margin-top: 5px;
}
</style>
</head>
<body>
<div id="map"></div>
<div class="coordinates-box">
<h3>鼠标坐标</h3>
<div class="coordinates-container">
<div class="label">经度:</div>
<div class="value" id="lng">0.000000</div>
<div class="label">纬度:</div>
<div class="value" id="lat">0.000000</div>
<div class="label">经度 (DMS):</div>
<div class="value dms-value" id="lng-dms">0° 0' 0" E</div>
<div class="label">纬度 (DMS):</div>
<div class="value dms-value" id="lat-dms">0° 0' 0" N</div>
<div class="label">缩放级别:</div>
<div class="value" id="zoom">0.00</div>
</div>
<button id="copy-coords" class="copy-button">复制坐标</button>
<div id="copy-message" class="copy-message">坐标已复制到剪贴板!</div>
</div>
<script>
const map = new maplibregl.Map({
container: 'map',
style: 'https://api.maptiler.com/maps/streets/style.json?key=get_your_own_OpIi9ZULNHzrESv6T2vL',
center: [0, 0],
zoom: 2
});
// 添加导航控件
map.addControl(new maplibregl.NavigationControl());
// 获取DOM元素
const lngElement = document.getElementById('lng');
const latElement = document.getElementById('lat');
const lngDmsElement = document.getElementById('lng-dms');
const latDmsElement = document.getElementById('lat-dms');
const zoomElement = document.getElementById('zoom');
const copyButton = document.getElementById('copy-coords');
const copyMessage = document.getElementById('copy-message');
// 当前坐标
let currentLng = 0;
let currentLat = 0;
// 监听鼠标移动
map.on('mousemove', function(e) {
// 更新当前坐标
currentLng = e.lngLat.lng;
currentLat = e.lngLat.lat;
// 更新十进制坐标显示
lngElement.textContent = currentLng.toFixed(6);
latElement.textContent = currentLat.toFixed(6);
// 更新度分秒坐标显示
lngDmsElement.textContent = convertToDMS(currentLng, 'lng');
latDmsElement.textContent = convertToDMS(currentLat, 'lat');
});
// 监听缩放变化
map.on('zoom', function() {
zoomElement.textContent = map.getZoom().toFixed(2);
});
// 初始设置缩放级别
map.on('load', function() {
zoomElement.textContent = map.getZoom().toFixed(2);
});
// 复制坐标按钮
copyButton.addEventListener('click', function() {
const textToCopy = `经度: ${currentLng.toFixed(6)}, 纬度: ${currentLat.toFixed(6)}`;
// 复制到剪贴板
navigator.clipboard.writeText(textToCopy).then(function() {
// 显示成功消息
copyMessage.style.display = 'block';
// 3秒后隐藏消息
setTimeout(function() {
copyMessage.style.display = 'none';
}, 3000);
});
});
// 将十进制度数转换为度分秒格式
function convertToDMS(value, type) {
const absolute = Math.abs(value);
const degrees = Math.floor(absolute);
const minutesNotTruncated = (absolute - degrees) * 60;
const minutes = Math.floor(minutesNotTruncated);
const seconds = Math.floor((minutesNotTruncated - minutes) * 60);
let direction = '';
if (type === 'lat') {
direction = value >= 0 ? 'N' : 'S';
} else {
direction = value >= 0 ? 'E' : 'W';
}
return `${degrees}° ${minutes}' ${seconds}" ${direction}`;
}
</script>
</body>
</html>