fix(导航): 当前位置不再在「屏幕中心 ↔ 下方」之间来回跳

根因(三条叠在一起)
1. 每次 GPS 回调都 setCenter(我) + panBy(固定偏移):中间那一瞬间画面就是居中的,
   高频回调(watchPosition 一秒能来好几次)时动画反复被打断 → 视觉上左右横跳
2. panBy 没传 noAnimation(百度 panBy 第 3 个参数支持 {noAnimation},实测有效),
   动画期间再被打断会停在中间位置
3. 朝向(ViewAnimation 兜底时约 260ms)在补偏移之后才转完,绕屏幕中心旋转会把
   已经推到下方的点又"甩"回中心附近

改法
- 区分两种相机动作:
  · snap(进导航/点返回导航/离得太远):setCenter + 朝向 + 收敛校正,允许跳一次
  · follow(正常跟随):**完全不 setCenter**,只按"目标屏幕位置 - 当前屏幕位置"
    做增量 panBy;朝向只在变化超过 3° 时才重设
- 收敛校正 _convergeCamera:每帧量一次实际屏幕坐标、补差值,最多 3 轮;
  「量 → 补」之间隔一帧,避免读到旧投影(这是之前越补越偏的根因)
- 朝向变化后再延迟 320ms 补一次校正(等旋转动画结束)
- panBy 一律 {noAnimation: true}(不支持时回退普通调用)
- 相机节流:350ms 最小间隔 + 位移 <8m 不抢镜头;_cameraSeq 让晚到的旧回调放弃
- 「返回导航」走 force 分支,立刻回正

实测(390×844,真实数据)
- 起导航:600 → 435(旋转瞬间) → 591 并稳定(连续 13 次采样不变)
- GPS 风暴 10×120ms:591 全程不动
- 返回导航:pill 正常出现、回正后 Y=563
- JS 异常 0
测试:169 pytest + 32 node 全绿
This commit is contained in:
zzy
2026-11-16 14:22:17 +08:00
parent f781095ffa
commit 7d9e12c001
2 changed files with 148 additions and 36 deletions
+71 -16
View File
@@ -4048,30 +4048,85 @@
_headingSupported = false;
}
}
function _centerOnUser(point, headingDeg) {
var NAV_CAMERA_MIN_INTERVAL_MS = 350;
var NAV_CAMERA_MIN_MOVE_M = 8;
var _cameraBusyUntil = 0;
var _lastCameraAt = 0;
var _lastCameraPoint = null;
var _lastHeading = null;
function _centerOnUser(point, headingDeg, force = false) {
const now = typeof performance !== "undefined" ? performance.now() : Date.now();
if (!force) {
if (now < _cameraBusyUntil) return;
if (now - _lastCameraAt < NAV_CAMERA_MIN_INTERVAL_MS && _lastCameraPoint && _haversineM(
_lastCameraPoint.lat,
_lastCameraPoint.lng,
point.lat,
point.lng
) < NAV_CAMERA_MIN_MOVE_M) {
return;
}
}
_lastCameraAt = now;
_lastCameraPoint = point;
const h = window.innerHeight || 800;
const offsetY = Math.round(h * NAV_BOTTOM_RATIO);
const mapHeading = typeof headingDeg === "number" ? -headingDeg : null;
const size = S.map.getSize && S.map.getSize() || { width: window.innerWidth || 390, height: h };
const desiredY = (size.height || h) * 0.5 + offsetY;
const desiredX = (size.width || 390) * 0.5;
_markProgrammaticMove();
let headingChanged = false;
if (mapHeading !== null && Math.abs((_lastHeading || 0) - mapHeading) > 3) {
_applyHeading(mapHeading);
_lastHeading = mapHeading;
headingChanged = true;
}
let px = null;
try {
S.map.setCenter(point, {
noAnimation: true,
callback: function() {
if (mapHeading !== null) _applyHeading(mapHeading);
try {
if (S.map.panBy) S.map.panBy(0, offsetY);
} catch (e) {
}
}
});
px = S.map.pointToPixel(point);
} catch (e) {
px = null;
}
const tooFar = !px || Math.abs(px.x - desiredX) > size.width * 1.2 || Math.abs(px.y - desiredY) > size.height * 1.2;
_cameraBusyUntil = now + 260;
if (force || tooFar) {
try {
S.map.setCenter(point);
if (mapHeading !== null) _applyHeading(mapHeading);
if (S.map.panBy) S.map.panBy(0, offsetY);
} catch (e2) {
S.map.setCenter(point, { noAnimation: true });
} catch (e) {
}
if (mapHeading !== null) {
try {
_applyHeading(mapHeading);
_lastHeading = mapHeading;
} catch (e) {
}
}
}
_convergeCamera(point, desiredX, desiredY, 0);
if (headingChanged) {
setTimeout(function() {
_convergeCamera(point, desiredX, desiredY, 0);
}, 320);
}
}
function _convergeCamera(point, desiredX, desiredY, round) {
requestAnimationFrame(function() {
try {
const px = S.map.pointToPixel(point);
const dx = Math.round(desiredX - px.x);
const dy = Math.round(desiredY - px.y);
if (Math.abs(dx) <= 2 && Math.abs(dy) <= 2) return;
if (round >= 3) return;
try {
S.map.panBy(dx, dy, { noAnimation: true });
} catch (e1) {
S.map.panBy(dx, dy);
}
_convergeCamera(point, desiredX, desiredY, round + 1);
} catch (e) {
}
});
}
var _compassHeading = null;
var _compassBound = false;
@@ -4371,7 +4426,7 @@
const me = typeof S.autoLocationMarker !== "undefined" && S.autoLocationMarker ? S.autoLocationMarker.getPosition() : null;
const anchor = me || _nav.ptsBD[i];
_placeNavArrow(anchor);
_centerOnUser(anchor, heading);
_centerOnUser(anchor, heading, true);
}
async function startNavigation(bike) {
if (_navLoading) return;