From 04ec8a67987036d0ac6c9b22765eaf445f8cd16c Mon Sep 17 00:00:00 2001 From: hwinkr Date: Sat, 27 Jan 2024 22:32:53 +0900 Subject: [PATCH] =?UTF-8?q?Feat(util=20function):=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=EC=9E=90=20=EC=9C=84=EC=B9=98=EC=99=80=20=EA=B4=80=EB=A0=A8?= =?UTF-8?q?=EB=90=9C=20=EC=9C=A0=ED=8B=B8=20=ED=95=A8=EC=88=98=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 위치 정보를 허용하는지, 사용자가 학교 내부에 있는지 확인하는 함수 구현 --- src/utils/map/user-location.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/utils/map/user-location.ts diff --git a/src/utils/map/user-location.ts b/src/utils/map/user-location.ts new file mode 100644 index 00000000..a6682e4f --- /dev/null +++ b/src/utils/map/user-location.ts @@ -0,0 +1,34 @@ +import { NO_PROVIDE_LOCATION, PKNU_MAP_CENTER } from '@constants/pknu-map'; +import { Location } from '@type/map'; + +const degreeToRadian = (deg: number) => deg * (Math.PI / 180); + +const getHaversineDistance = (lat: number, lng: number) => { + const R = 6371000; + + const dLat = degreeToRadian(PKNU_MAP_CENTER.LAT - lat); + const dLon = degreeToRadian(PKNU_MAP_CENTER.LNG - lng); + + const halfSideLength = + Math.sin(dLat / 2) * Math.sin(dLat / 2) + + Math.cos(degreeToRadian(PKNU_MAP_CENTER.LAT)) * + Math.cos(degreeToRadian(lat)) * + Math.sin(dLon / 2) * + Math.sin(dLon / 2); + + const angularDistance = + 2 * Math.atan2(Math.sqrt(halfSideLength), Math.sqrt(1 - halfSideLength)); + + return R * angularDistance; +}; + +export const hasLocationPermission = (location: Location | null) => { + return ( + location && JSON.stringify(location) !== JSON.stringify(NO_PROVIDE_LOCATION) + ); +}; + +export const isUserInShcool = (lat: number, lng: number) => { + const maxDistance = 450; + return getHaversineDistance(lat, lng) <= maxDistance; +};