fix: xoay màn hình theo hướng người nhìn trong màn hình bản đồ đã mượt nhưng đến góc là xoay tròn

This commit is contained in:
2026-06-17 22:36:05 +07:00
parent 2462850e2e
commit e7b8c672e0
+26 -11
View File
@@ -387,23 +387,38 @@ export const TourDetailPage = ({
// Theo dõi hướng thiết bị (la bàn) // Theo dõi hướng thiết bị (la bàn)
useEffect(() => { useEffect(() => {
if (window.DeviceOrientationEvent) { const handleOrientation = (event: any) => {
const handleDeviceOrientation = (event: DeviceOrientationEvent) => { let heading: number | null = null;
// Sử dụng webkitCompassHeading cho iOS nếu có, nếu không thì dùng alpha
const heading = (event as any).webkitCompassHeading || event.alpha; // 1. Đối với iOS: Sử dụng webkitCompassHeading (đã chuẩn hóa hướng Bắc thực)
if (heading !== null && heading !== undefined) { if (event.webkitCompassHeading !== undefined && event.webkitCompassHeading !== null) {
heading = event.webkitCompassHeading;
}
// 2. Đối với Android (Chrome): Cần kiểm tra tính tuyệt đối của dữ liệu
else if (event.alpha !== null && event.alpha !== undefined) {
// Chrome trên Android chỉ cung cấp hướng la bàn chuẩn khi event.absolute là true
// hoặc khi nhận từ sự kiện 'deviceorientationabsolute'
if (event.absolute === true || event.type === 'deviceorientationabsolute') {
// Alpha trên Android tăng theo chiều ngược kim đồng hồ (0=North, 90=West)
// Cần chuyển đổi sang chiều kim đồng hồ để khớp với logic quay bản đồ
heading = (360 - event.alpha) % 360;
}
}
if (heading !== null) {
setDeviceOrientationHeading(heading); setDeviceOrientationHeading(heading);
} }
}; };
window.addEventListener('deviceorientation', handleDeviceOrientation); // Đăng ký cả hai loại sự kiện để hỗ trợ tối đa các dòng điện thoại
window.addEventListener('deviceorientation', handleOrientation, true);
window.addEventListener('deviceorientationabsolute', handleOrientation, true);
return () => { return () => {
window.removeEventListener('deviceorientation', handleDeviceOrientation); window.removeEventListener('deviceorientation', handleOrientation, true);
window.removeEventListener('deviceorientationabsolute', handleOrientation, true);
}; };
} else { }, []);
setDeviceOrientationHeading(null);
}
}, []); // Chạy một lần khi component mount
// Logic kết hợp để xác định hướng xoay bản đồ // Logic kết hợp để xác định hướng xoay bản đồ
useEffect(() => { useEffect(() => {