From 7d9e12c0016ce5da6ee9c4a03a7abf95677938e3 Mon Sep 17 00:00:00 2001 From: NekoYume517 <3146527494@qq.com> Date: Mon, 16 Nov 2026 14:22:17 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E5=AF=BC=E8=88=AA):=20=E5=BD=93=E5=89=8D?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE=E4=B8=8D=E5=86=8D=E5=9C=A8=E3=80=8C=E5=B1=8F?= =?UTF-8?q?=E5=B9=95=E4=B8=AD=E5=BF=83=20=E2=86=94=20=E4=B8=8B=E6=96=B9?= =?UTF-8?q?=E3=80=8D=E4=B9=8B=E9=97=B4=E6=9D=A5=E5=9B=9E=E8=B7=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因(三条叠在一起) 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 全绿 --- src-ts/nav/navigation.ts | 97 +++++++++++++++++++++++++++++++--------- static/dist/app.js | 87 ++++++++++++++++++++++++++++------- 2 files changed, 148 insertions(+), 36 deletions(-) diff --git a/src-ts/nav/navigation.ts b/src-ts/nav/navigation.ts index 8d2e260..31cd243 100644 --- a/src-ts/nav/navigation.ts +++ b/src-ts/nav/navigation.ts @@ -234,30 +234,86 @@ export function _applyHeading(deg) { //(实测方位角 145° 时:setHeading(145) 前方在左上;setHeading(-145) 前方正上方) // static/js/navigation.js:303-330 -export function _centerOnUser(point, headingDeg) { +// 相机节流:GPS 高频回调(watchPosition 一秒能来好几次)时,如果每次 +// 都 setCenter 到正中再平移到下方,动画还没播完就被下一次打断, +// 视觉上就是「中间 ↔ 下方」来回跳。 +const NAV_CAMERA_MIN_INTERVAL_MS = 350; // 两次相机移动的最小间隔 +const NAV_CAMERA_MIN_MOVE_M = 8; // 位置几乎没变就不抢镜头 +let _cameraBusyUntil = 0; // setCenter 回调还没落地 +let _lastCameraAt = 0; +let _lastCameraPoint: any = null; +let _cameraSeq = 0; // 每次相机移动自增,晚到的旧回调直接放弃 +let _lastHeading = null; // 已应用到地图的朝向(避免每帧重设) + +export 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(); - 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) {} - } - }); - } catch (e) { - // 老版本 setCenter 不支持 options,退回手动顺序 - try { - S.map.setCenter(point); - if (mapHeading !== null) _applyHeading(mapHeading); - if (S.map.panBy) S.map.panBy(0, offsetY); - } catch (e2) { /* 忽略 */ } + + // 朝向变了先转(panBy 不会重置朝向;setCenter 会,所以先不动中心) + let headingChanged = false; + if (mapHeading !== null && Math.abs((_lastHeading || 0) - mapHeading) > 3) { + _applyHeading(mapHeading); + _lastHeading = mapHeading; + headingChanged = true; } + + // 当前位置离目标太远(刚进导航/从后台回来/点了返回导航)→ 重新居中一次 + let px = null; + try { 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, { noAnimation: true }); } catch (e) {} + if (mapHeading !== null) { + try { _applyHeading(mapHeading); _lastHeading = mapHeading; } catch (e) {} + } + } + + // 收敛式校正:每帧量一次实际位置,补差值,最多迭代 3 帧。 + // 关键是「量 → 补」之间隔一帧,避免读到旧投影导致越补越偏(之前来回跳的根因)。 + _convergeCamera(point, desiredX, desiredY, 0); + // 旋转是围绕屏幕中心做的:如果朝向用了动画(ViewAnimation ~260ms), + // 会在我们补完偏移之后才转完,把已经推到下方的点又"甩"回中心附近。 + // 转完再补一次,就稳了。 + 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; // 收敛不了就放弃这一帧,下次 tick 再来 + try { S.map.panBy(dx, dy, { noAnimation: true }); } + catch (e1) { S.map.panBy(dx, dy); } + _convergeCamera(point, desiredX, desiredY, round + 1); + } catch (e) {} + }); } // ========== 罗盘 + 导航箭头 ========== @@ -630,7 +686,8 @@ export function resumeNavigation() { ? S.autoLocationMarker.getPosition() : null; const anchor = me || _nav.ptsBD[i]; _placeNavArrow(anchor); - _centerOnUser(anchor, heading); + // 用户主动点「返回导航」:绕过节流,立刻回正 + _centerOnUser(anchor, heading, true); } // static/js/navigation.js:666-760 diff --git a/static/dist/app.js b/static/dist/app.js index b03b62b..d8624d1 100644 --- a/static/dist/app.js +++ b/static/dist/app.js @@ -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;