Files
gmTouringMiniApp/test/map-viewport.test.ts
T
周瑞哲 7ef4094788
AI Code Review / review (pull_request) Successful in 2m11s
fix(map): 收敛 regionchange 回写回路,修复选中标点后地图抽动
选中标点后地图在左右/上下反复抽动,根因是视口回写形成了自激回路:
map 的 longitude/latitude/scale 绑定到 store,而 regionchange 又无条件把
地图当前中心点写回 store —— 写 store 触发地图移动,移动结束回传一个
「差一点点」的中心点,再写回,再移动。上一个提交把中心点改成从
detail.centerLocation 同步读取后,回路里原本靠 getCenterLocation 异步跳变
掩盖住的这一点噪声就直接闭合了。

两道闸:
- 只接受手势造成的变化。causedBy 为 update(我们自己改绑定值或调
  includePoints 触发)时 store 已是权威值,不回写。老基础库拿不到
  causedBy 时退化到下一道闸。
- 所有回写走 commitViewport,经 isSignificantViewportChange 过滤掉小于
  1e-4 度 / 0.01 级的变化。远小于任何一次真实拖动,足以吸收量化噪声。

顺带修一个 H5 构建回归(1bcdd40 引入):uni 的 H5 路由生成器按路径推导
组件标识,pages/poi/detail 与 pages-poi/detail 都归一成 PagesPoiDetail,
重复声明导致 build:h5 失败。兼容跳板必须留在旧路径上,所以改名分包路由
pages-poi/detail -> pages-poi/poi-detail。
2026-08-03 11:08:39 +08:00

34 lines
1.4 KiB
TypeScript

import type { MapViewport } from '@/domain/poi'
import { describe, expect, it } from 'vitest'
import { isSignificantViewportChange } from '@/services/map'
const current: MapViewport = { longitude: 113.935, latitude: 22.748, scale: 13 }
function shifted(delta: Partial<MapViewport>): MapViewport {
return {
longitude: current.longitude + (delta.longitude ?? 0),
latitude: current.latitude + (delta.latitude ?? 0),
scale: current.scale + (delta.scale ?? 0),
}
}
describe('isSignificantViewportChange', () => {
it('丢弃地图回传中心点的量化噪声', () => {
expect(isSignificantViewportChange(current, shifted({}))).toBe(false)
expect(isSignificantViewportChange(current, shifted({ longitude: 1e-6 }))).toBe(false)
expect(isSignificantViewportChange(current, shifted({ latitude: -1e-6 }))).toBe(false)
expect(isSignificantViewportChange(current, shifted({ scale: 0.001 }))).toBe(false)
})
it('接受真实拖动与缩放', () => {
expect(isSignificantViewportChange(current, shifted({ longitude: 0.01 }))).toBe(true)
expect(isSignificantViewportChange(current, shifted({ latitude: -0.01 }))).toBe(true)
expect(isSignificantViewportChange(current, shifted({ scale: 1 }))).toBe(true)
})
it('单个维度超阈值就算变化', () => {
const next = shifted({ longitude: 1e-6, latitude: 1e-6, scale: 2 })
expect(isSignificantViewportChange(current, next)).toBe(true)
})
})