|
| 1 | +import { Callout } from 'nextra/components'; |
| 2 | + |
| 3 | +# useGeolocation |
| 4 | + |
| 5 | +<Callout type="info"> |
| 6 | + ์ด ํ
์ ๋ธ๋ผ์ฐ์ ์ Geolocation API๋ฅผ ์ฌ์ฉํ์ฌ ์ฌ์ฉ์์ ์์น ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ต๋๋ค. |
| 7 | + ์ง์๋์ง ์๋ ๋ธ๋ผ์ฐ์ ์์๋ ์ ํํ ์์น ์ ๋ณด๋ฅผ ์ ๊ณตํ์ง ์์ ์ ์์ต๋๋ค. |
| 8 | +</Callout> |
| 9 | + |
| 10 | +## Introduce |
| 11 | + |
| 12 | +`useGeolocation` ํ
์ ์ฌ์ฉ์์ ์์น ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ ์ํ๋ฅผ ๊ด๋ฆฌํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค. |
| 13 | +์ด ํ
์ ์์น ์ ๋ณด์ ์๋, ๊ฒฝ๋, ๊ณ ๋ ๋ฑ ๋ค์ํ ์์น ๋ฐ์ดํฐ๋ฅผ ๋ฐํํ๋ฉฐ, ์์น ์ ๋ณด ์์ง ์ค ๋ฐ์ํ ์ค๋ฅ๋ ํจ๊ป ๋ฐํํฉ๋๋ค. |
| 14 | + |
| 15 | +```ts |
| 16 | +export interface UseGeolocationReturnType { |
| 17 | + loading: boolean; |
| 18 | + error: GeolocationPositionError | null; |
| 19 | + timestamp?: EpochTimeStamp; |
| 20 | + latitude?: number; |
| 21 | + longitude?: number; |
| 22 | + altitude?: number; |
| 23 | + accuracy?: number; |
| 24 | + altitudeAccuracy?: number; |
| 25 | + heading?: number; |
| 26 | + speed?: number; |
| 27 | +} |
| 28 | + |
| 29 | +const useGeolocation = ( |
| 30 | + options?: PositionOptions |
| 31 | +): UseGeolocationReturnType; |
| 32 | +``` |
| 33 | + |
| 34 | +### Props |
| 35 | + |
| 36 | +- `options` : ์์น ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ฌ ๋ ์ฌ์ฉํ ์ต์
|
| 37 | + - `enableHighAccuracy` : ์์น ์ ๋ณด๋ฅผ ๋์ ์ ํ๋๋ก ์์งํ ์ง ์ฌ๋ถ (๊ธฐ๋ณธ๊ฐ: `false`) |
| 38 | + - `timeout` : ์์น ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๊ธฐ ์ํด ๋๊ธฐํ ์ต๋ ์๊ฐ (๋ฐ๋ฆฌ์ด ๋จ์) |
| 39 | + - `maximumAge` : ์์น ์ ๋ณด๋ฅผ ์บ์ฑํ ์ต๋ ์๊ฐ (๋ฐ๋ฆฌ์ด ๋จ์) |
| 40 | + |
| 41 | +### Returns |
| 42 | + |
| 43 | +- `loading: boolean` : ์์น ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋ ์ค์ธ์ง ์ฌ๋ถ |
| 44 | +- `error: GeolocationPositionError | null` : ๋ฐ์ํ ์ค๋ฅ |
| 45 | +- `timestamp?: EpochTimeStamp` : ์์น ์ ๋ณด์ ํ์์คํฌํ |
| 46 | +- `latitude?: number` : ํ์ฌ ์์น์ ์๋ |
| 47 | +- `longitude?: number` : ํ์ฌ ์์น์ ๊ฒฝ๋ |
| 48 | +- `altitude?: number` : ํ์ฌ ์์น์ ๊ณ ๋ |
| 49 | +- `accuracy?: number` : ์์น ์ ๋ณด์ ์ ํ๋ |
| 50 | +- `altitudeAccuracy?: number` : ๊ณ ๋ ์ ๋ณด์ ์ ํ๋ |
| 51 | +- `heading?: number` : ์ด๋ ๋ฐฉํฅ |
| 52 | +- `speed?: number` : ํ์ฌ ์๋ |
| 53 | + |
| 54 | +## Examples |
| 55 | + |
| 56 | +```ts copy filename="GeolocationComponent.tsx" |
| 57 | +import useGeolocation from './useGeolocation'; |
| 58 | + |
| 59 | +const GeolocationComponent = () => { |
| 60 | + const { |
| 61 | + latitude, |
| 62 | + longitude, |
| 63 | + altitude, |
| 64 | + accuracy, |
| 65 | + altitudeAccuracy, |
| 66 | + heading, |
| 67 | + speed, |
| 68 | + timestamp, |
| 69 | + error, |
| 70 | + loading, |
| 71 | + } = useGeolocation({ enableHighAccuracy: true }); |
| 72 | + |
| 73 | + if (loading) { |
| 74 | + return <p>์์น ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋ ์ค์
๋๋ค...</p>; |
| 75 | + } |
| 76 | + |
| 77 | + if (error) { |
| 78 | + return <p>์ค๋ฅ ๋ฐ์: {error.message}</p>; |
| 79 | + } |
| 80 | + |
| 81 | + return ( |
| 82 | + <div> |
| 83 | + <h2>์์น ์ ๋ณด</h2> |
| 84 | + <p>์๋: {latitude}</p> |
| 85 | + <p>๊ฒฝ๋: {longitude}</p> |
| 86 | + <p>๊ณ ๋: {altitude}</p> |
| 87 | + <p>์ ํ๋: {accuracy} meters</p> |
| 88 | + <p>๊ณ ๋ ์ ํ๋: {altitudeAccuracy} meters</p> |
| 89 | + <p>๋ฐฉํฅ: {heading} degrees</p> |
| 90 | + <p>์๋: {speed} m/s</p> |
| 91 | + <p>ํ์์คํฌํ: {new Date(timestamp || 0).toLocaleTimeString()}</p> |
| 92 | + </div> |
| 93 | + ); |
| 94 | +}; |
| 95 | +``` |
0 commit comments