import type { MapViewport } from '@/domain/poi' /** * 约 1e-4 度 ≈ 11 米。原生地图把中心点量化到像素,回传的中心点和我们写进去的 * 值总会差一点点;这个阈值用来吸收那点量化噪声,同时远小于任何一次真实拖动。 */ const COORDINATE_EPSILON = 1e-4 const SCALE_EPSILON = 0.01 /** * 判断地图回传的视口是否真的变了,值得写回 store。 * * 地图的 longitude/latitude/scale 是绑定到 store 的,而 `regionchange` 又会把 * 地图当前中心点回写 store —— 这是一个闭环。选中标点后我们把视口设到 POI 坐标, * 地图动画结束回传一个「差一点点」的中心点,无条件写回就会再次改动绑定值、 * 触发地图移动、再回传……表现出来就是地图在左右或上下反复抽动。 * * 回写本身是需要的(用户拖完地图,store 要记住新位置,返回页面时才能恢复), * 所以这里不是禁止回写,而是把小于阈值的变化当作噪声丢掉,让闭环收敛。 */ export function isSignificantViewportChange(current: MapViewport, next: MapViewport): boolean { return Math.abs(next.longitude - current.longitude) >= COORDINATE_EPSILON || Math.abs(next.latitude - current.latitude) >= COORDINATE_EPSILON || Math.abs(next.scale - current.scale) >= SCALE_EPSILON }