Skip to content

Commit

Permalink
Feat(util function): 사용자 위치와 관련된 유틸 함수 구현
Browse files Browse the repository at this point in the history
- 위치 정보를 허용하는지, 사용자가 학교 내부에 있는지 확인하는 함수 구현
  • Loading branch information
hwinkr committed Jan 27, 2024
1 parent d9d872e commit 04ec8a6
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/utils/map/user-location.ts
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit 04ec8a6

Please sign in to comment.