From 6fabbcfe5ca08a4eae74b2cfdb89a0d07aab4505 Mon Sep 17 00:00:00 2001 From: rayd1o Date: Thu, 19 Mar 2026 12:49:38 +0800 Subject: [PATCH] feat(earth): request geolocation on resetView, fallback to China --- frontend/public/earth/js/controls.js | 52 +++++++++++++++++----------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/frontend/public/earth/js/controls.js b/frontend/public/earth/js/controls.js index 3dfec120..cb36c094 100644 --- a/frontend/public/earth/js/controls.js +++ b/frontend/public/earth/js/controls.js @@ -153,27 +153,39 @@ function animateValue(start, end, duration, onUpdate, onComplete) { export function resetView(camera) { if (!earthObj) return; - const latRot = EARTH_CONFIG.chinaLat * Math.PI / 180; - const targetRotX = EARTH_CONFIG.tiltRad + latRot * EARTH_CONFIG.latCoefficient; - const targetRotY = -(EARTH_CONFIG.chinaRotLon * Math.PI / 180); - - const startRotX = earthObj.rotation.x; - const startRotY = earthObj.rotation.y; - const startZoom = zoomLevel; - const targetZoom = 1.0; - - animateValue(0, 1, 800, (progress) => { - const ease = 1 - Math.pow(1 - progress, 3); - earthObj.rotation.x = startRotX + (targetRotX - startRotX) * ease; - earthObj.rotation.y = startRotY + (targetRotY - startRotY) * ease; + function animateToView(targetLat, targetLon, targetRotLon) { + const latRot = targetLat * Math.PI / 180; + const targetRotX = EARTH_CONFIG.tiltRad + latRot * EARTH_CONFIG.latCoefficient; + const targetRotY = -(targetRotLon * Math.PI / 180); - zoomLevel = startZoom + (targetZoom - startZoom) * ease; - camera.position.z = CONFIG.defaultCameraZ / zoomLevel; - updateZoomDisplay(zoomLevel, camera.position.z.toFixed(0)); - }, () => { - zoomLevel = 1.0; - showStatusMessage('视角已重置', 'info'); - }); + const startRotX = earthObj.rotation.x; + const startRotY = earthObj.rotation.y; + const startZoom = zoomLevel; + const targetZoom = 1.0; + + animateValue(0, 1, 800, (progress) => { + const ease = 1 - Math.pow(1 - progress, 3); + earthObj.rotation.x = startRotX + (targetRotX - startRotX) * ease; + earthObj.rotation.y = startRotY + (targetRotY - startRotY) * ease; + + zoomLevel = startZoom + (targetZoom - startZoom) * ease; + camera.position.z = CONFIG.defaultCameraZ / zoomLevel; + updateZoomDisplay(zoomLevel, camera.position.z.toFixed(0)); + }, () => { + zoomLevel = 1.0; + showStatusMessage('视角已重置', 'info'); + }); + } + + if (navigator.geolocation) { + navigator.geolocation.getCurrentPosition( + (pos) => animateToView(pos.coords.latitude, pos.coords.longitude, -pos.coords.longitude), + () => animateToView(EARTH_CONFIG.chinaLat, EARTH_CONFIG.chinaLon, EARTH_CONFIG.chinaRotLon), + { timeout: 5000, enableHighAccuracy: false } + ); + } else { + animateToView(EARTH_CONFIG.chinaLat, EARTH_CONFIG.chinaLon, EARTH_CONFIG.chinaRotLon); + } if (typeof window.clearLockedCable === 'function') { window.clearLockedCable();