首页 / MapLibre / API 文档
MapMouseEvent
先看结论
鼠标与触摸事件回调收到的对象,同时携带地理坐标与屏幕坐标。
map.on(type, listener) → MapMouseEvent
事件对象字段
| 字段 | 类型 | 说明 |
|---|---|---|
lngLat | LngLat | 事件位置对应的经纬度 |
point | Point | 相对地图容器的像素坐标 |
originalEvent | Event | 原生 DOM 事件,可读 shiftKey 等修饰键 |
features | Feature[] | 图层级事件才有,含该点位下可见的要素 |
type | string | 事件名 |
target | Map | 触发事件的地图实例 |
preventDefault() | 方法 | 阻止默认行为,如避免双击选中文本 |
事件类型
| 事件 | 触发条件 |
|---|---|
click | 单击 |
dblclick | 双击 |
mousedown / mouseup | 按下与抬起 |
mousemove | 指针移动,高频 |
contextmenu | 右键菜单 |
mouseenter / mouseleave | 仅图层级事件,进入或离开要素 |
touchstart / touchend | 触摸屏手势 |
地图级与图层级
地图级事件只给位置,不给要素。图层级事件把图层 id 作为第二个参数,
回调里才有 features,且只包含该图层命中的要素。
/* 地图级:只有位置 */
map.on('click', function (e) {
console.log(e.lngLat.toArray());
});
/* 图层级:带要素 */
map.on('click', 'station-dots', function (e) {
var f = e.features[0];
console.log(f.properties.name, e.lngLat.toArray());
});
e.features[0].geometry 是简化后的几何,用于判断足够,
不要拿它做精确面积或拓扑计算。需要原始坐标时按属性回查源数据。
手动查询要素
需要一次拿到多个图层的命中结果,用
map.queryRenderedFeatures,参数可以是像素点或屏幕矩形。
map.on('click', function (e) {
var hits = map.queryRenderedFeatures(e.point, {
layers: ['station-dots', 'zones-fill']
});
hits.forEach(function (f) {
console.log(f.layer.id, f.properties);
});
});
高频事件的处理
mousemove 每帧触发。在回调里做查询或重计算会直接拖慢地图。
需要实时坐标时把结果写进一个固定节点,或者用
requestAnimationFrame 合并处理:
var pending = null;
map.on('mousemove', function (e) {
pending = e.lngLat;
if (!pending.__queued) {
pending.__queued = true;
requestAnimationFrame(function () {
label.textContent = pending.lng.toFixed(4) + ', ' + pending.lat.toFixed(4);
});
}
});
指针样式也要成对设置。只在 mouseenter 改光标、
忘了在 mouseleave 改回来,是交互里最常见的残留问题。
事件绑定依赖图层存在。在 load 之前绑定图层级事件会静默失效;
样式切换后图层重建,事件需要重新绑定。
经纬志(2026)。《MapMouseEvent》。https://www.jingweizhi.com/maplibre/api/map-mouse-event.html(访问日期 2026-11-03)。