获取鼠标指针下的要素
获取鼠标指针下的地图要素;
<!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%; }
.info-panel {
position: absolute;
bottom: 20px;
right: 20px;
width: 300px;
max-height: 50%;
overflow-y: auto;
background-color: rgba(255, 255, 255, 0.9);
padding: 15px;
border-radius: 4px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
font-family: 'Arial', sans-serif;
font-size: 14px;
z-index: 1;
}
.info-panel h3 {
margin: 0 0 10px 0;
font-size: 16px;
border-bottom: 1px solid #ddd;
padding-bottom: 5px;
}
.feature-list {
list-style-type: none;
padding: 0;
margin: 0;
}
.feature-item {
margin-bottom: 10px;
padding: 8px;
background-color: #f8f8f8;
border-radius: 4px;
border-left: 3px solid #4264fb;
}
.feature-layer {
font-weight: bold;
color: #4264fb;
margin-bottom: 3px;
}
.feature-id {
color: #888;
font-size: 12px;
margin-bottom: 5px;
}
.feature-properties {
font-size: 12px;
color: #333;
overflow-wrap: break-word;
word-wrap: break-word;
}
.no-features {
color: #888;
font-style: italic;
}
.info-message {
margin-top: 15px;
font-size: 12px;
color: #666;
line-height: 1.4;
}
</style>
</head>
<body>
<div id="map"></div>
<div class="info-panel">
<h3>鼠标指针下的要素</h3>
<div id="features">
<p class="no-features">将鼠标移到地图上的要素上...</p>
</div>
<div class="info-message">
此示例使用 <code>queryRenderedFeatures()</code> 方法获取鼠标指针下的地图要素。移动鼠标来查看不同要素的信息。
</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());
// 获取要素显示容器元素
const featuresContainer = document.getElementById('features');
// 监听鼠标移动事件
map.on('mousemove', (e) => {
// 查询鼠标指针位置的渲染要素
const features = map.queryRenderedFeatures(e.point);
// 清除旧的要素信息
featuresContainer.innerHTML = '';
if (features.length === 0) {
// 如果没有要素,显示提示信息
featuresContainer.innerHTML = '<p class="no-features">当前位置没有要素</p>';
return;
}
// 创建要素列表
const featuresList = document.createElement('ul');
featuresList.className = 'feature-list';
// 最多显示5个要素,以避免信息过多
const maxFeatures = Math.min(5, features.length);
for (let i = 0; i < maxFeatures; i++) {
const feature = features[i];
// 创建要素项
const item = document.createElement('li');
item.className = 'feature-item';
// 添加图层信息
const layerDiv = document.createElement('div');
layerDiv.className = 'feature-layer';
layerDiv.textContent = `图层: ${feature.layer.id}`;
item.appendChild(layerDiv);
// 添加要素ID(如果存在)
if (feature.id) {
const idDiv = document.createElement('div');
idDiv.className = 'feature-id';
idDiv.textContent = `ID: ${feature.id}`;
item.appendChild(idDiv);
}
// 添加要素属性(如果存在)
if (feature.properties && Object.keys(feature.properties).length > 0) {
const propsDiv = document.createElement('div');
propsDiv.className = 'feature-properties';
// 限制显示的属性数量,最多显示5个
const propertyKeys = Object.keys(feature.properties).slice(0, 5);
// 创建属性表格
let propsHtml = '<strong>属性:</strong><br>';
propertyKeys.forEach(key => {
// 跳过空值和太长的值
const value = feature.properties[key];
if (value !== null && value !== undefined) {
// 对值进行截断,避免过长
const displayValue = typeof value === 'string' && value.length > 30
? value.substring(0, 30) + '...'
: value;
propsHtml += `${key}: ${displayValue}<br>`;
}
});
// 如果有更多属性未显示,添加提示
if (Object.keys(feature.properties).length > 5) {
propsHtml += '...(还有更多属性)';
}
propsDiv.innerHTML = propsHtml;
item.appendChild(propsDiv);
}
// 将要素项添加到列表
featuresList.appendChild(item);
}
// 如果有更多要素未显示,添加提示
if (features.length > maxFeatures) {
const moreItem = document.createElement('li');
moreItem.className = 'feature-item';
moreItem.innerHTML = `<div class="feature-layer">...还有 ${features.length - maxFeatures} 个要素未显示</div>`;
featuresList.appendChild(moreItem);
}
// 将要素列表添加到容器
featuresContainer.appendChild(featuresList);
});
</script>
</body>
</html>