-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(util function): 사용자 위치와 관련된 유틸 함수 구현
- 위치 정보를 허용하는지, 사용자가 학교 내부에 있는지 확인하는 함수 구현
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |