|
| 1 | +import { Steps } from 'nextra/components' |
| 2 | +import AvatarCircles from "@/components/magic-ui/avatar.circles.tsx"; |
| 3 | + |
| 4 | +# useGeolocation |
| 5 | + |
| 6 | +<h3 className="mt-4 text-xl font-normal text-gray-400">A custom hook that provides functionalities for accessing and managing geolocation data.</h3> |
| 7 | + |
| 8 | +### Add hook |
| 9 | + |
| 10 | +Create a file `use-geolocation.ts` and copy & paste the code from [useGeolocation](/hooks/use-geolocation#hook). |
| 11 | + |
| 12 | +### Hook |
| 13 | + |
| 14 | +```ts copy |
| 15 | +import { useState, useEffect, useCallback } from 'react' |
| 16 | + |
| 17 | +type Coordinates = { |
| 18 | + latitude: number | null |
| 19 | + longitude: number | null |
| 20 | +} |
| 21 | + |
| 22 | +type GeolocationError = { |
| 23 | + code: number |
| 24 | + message: string |
| 25 | +} | null |
| 26 | + |
| 27 | +type GeolocationOptions = { |
| 28 | + enableHighAccuracy?: boolean |
| 29 | + timeout?: number |
| 30 | + maximumAge?: number |
| 31 | +} |
| 32 | + |
| 33 | +export const useGeolocation = (options?: GeolocationOptions) => { |
| 34 | + const [coordinates, setCoordinates] = useState<Coordinates>({ |
| 35 | + latitude: null, |
| 36 | + longitude: null, |
| 37 | + }) |
| 38 | + |
| 39 | + const [error, setError] = useState<GeolocationError>(null) |
| 40 | + |
| 41 | + const handleSuccess = (position: GeolocationPosition) => { |
| 42 | + const { latitude, longitude } = position.coords |
| 43 | + setCoordinates({ latitude, longitude }) |
| 44 | + setError(null) |
| 45 | + } |
| 46 | + |
| 47 | + const handleError = (error: GeolocationPositionError) => { |
| 48 | + setError({ |
| 49 | + code: error.code, |
| 50 | + message: error.message, |
| 51 | + }) |
| 52 | + } |
| 53 | + |
| 54 | + const getCurrentPosition = useCallback(() => { |
| 55 | + if (navigator.geolocation) { |
| 56 | + navigator.geolocation.getCurrentPosition(handleSuccess, handleError, options) |
| 57 | + } else { |
| 58 | + setError({ |
| 59 | + code: 0, |
| 60 | + message: 'Geolocation is not supported by your browser.', |
| 61 | + }) |
| 62 | + } |
| 63 | + }, [options]) |
| 64 | + |
| 65 | + useEffect(() => { |
| 66 | + getCurrentPosition() |
| 67 | + }, [getCurrentPosition]) |
| 68 | + |
| 69 | + return { coordinates, error, refresh: getCurrentPosition } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +### Usage |
| 74 | + |
| 75 | +```jsx copy |
| 76 | +import React from 'react'; |
| 77 | +import { useGeolocation } from './hooks/use-geolocation'; |
| 78 | + |
| 79 | +function GeolocationComponent() { |
| 80 | + const { coordinates, error, refresh } = useGeolocation(); |
| 81 | + |
| 82 | + if (error) { |
| 83 | + return <div>Error: {error.message}</div>; |
| 84 | + } |
| 85 | + |
| 86 | + return ( |
| 87 | + <div> |
| 88 | + <h1>Geolocation Demo</h1> |
| 89 | + {coordinates.latitude && coordinates.longitude ? ( |
| 90 | + <p> |
| 91 | + Latitude: {coordinates.latitude}, Longitude: {coordinates.longitude} |
| 92 | + </p> |
| 93 | + ) : ( |
| 94 | + <p>Loading location...</p> |
| 95 | + )} |
| 96 | + <button onClick={refresh}>Refresh Location</button> |
| 97 | + </div> |
| 98 | + ); |
| 99 | +} |
| 100 | + |
| 101 | +export default GeolocationComponent; |
| 102 | +``` |
| 103 | + |
| 104 | +## API |
| 105 | + |
| 106 | +<h1 className='text-xl font-medium mt-6'>Returns</h1> |
| 107 | + |
| 108 | +| Name | Description | |
| 109 | +|------|-------------| |
| 110 | +| `coordinates` | An object containing the current latitude and longitude. | |
| 111 | +| `error` | An object containing error information if geolocation fails. | |
| 112 | +| `refresh` | A function to manually refresh the geolocation. | |
| 113 | + |
| 114 | +## Contributors |
| 115 | + |
| 116 | +<AvatarCircles numPeople={1} avatarUrls={["https://avatars.githubusercontent.com/u/20143684?v=4"]} className="mt-6"/> |
0 commit comments