Skip to content

Commit

Permalink
watch location テスト
Browse files Browse the repository at this point in the history
  • Loading branch information
KokiKubota2 committed Jul 24, 2024
1 parent 302d6b0 commit 563b688
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AppBar, AuthProvider } from 'app'
import { Box, Container } from 'components/mui/material'

import { AppBar, AuthProvider } from 'app'
import { GeolocationProvider } from 'lib/context/geolocation'

export const metadata = {
title: 'MIL MEMO',
Expand All @@ -14,7 +15,9 @@ const RootLayout = ({ children }: { children: React.ReactNode }) => (
<AppBar />
</Box>
<Container>
<AuthProvider>{children}</AuthProvider>
<AuthProvider>
<GeolocationProvider>{/* {children} */}</GeolocationProvider>
</AuthProvider>
</Container>
</body>
</html>
Expand Down
99 changes: 99 additions & 0 deletions src/lib/context/geolocation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
'use client'

import _ from 'lodash'

import {
createContext,
ReactNode,
useContext,
useEffect,
useState,
} from 'react'

type LocationProps = {
latitude: number
longitude: number
accuracy: number
altitude: number | null
altitudeAccuracy: number | null
heading: number | null
speed: number | null
timestamp: number | null
}

type LocationContextProps = { currentLocation: LocationProps | null }

const DefaultGeolocationContextValue = { currentLocation: null }

const GeolocationContext = createContext<LocationContextProps>(
DefaultGeolocationContextValue
)

const MAXIMUM_AGE_MS = 300000
const TIMEOUT_MS = 10000

export const GeolocationProvider: React.FC<{ children: ReactNode }> = ({
children,
}) => {
const [currentLocation, setCurrentLocation] = useState<LocationProps | null>(
null
)
const [loading, setLoading] = useState(true)
const [error, setError] = useState('')

console.log('currentLocation', currentLocation)

useEffect(() => {
console.log(typeof window !== 'undefined' && 'geolocation' in navigator)

const onSuccess = (position: GeolocationPosition) => {
setCurrentLocation({
..._.pick(position.coords, [
'latitude',
'longitude',
'accuracy',
'altitude',
'altitudeAccuracy',
'heading',
'speed',
]),
timestamp: position.timestamp,
})
setLoading(false)
}

const onError = (error: GeolocationPositionError) => {
console.error({ error })
setError(error.message)
setLoading(false)
}

const watchId = navigator.geolocation.watchPosition(onSuccess, onError, {
enableHighAccuracy: true,
timeout: TIMEOUT_MS,
maximumAge: MAXIMUM_AGE_MS,
})

return () => {
navigator.geolocation.clearWatch(watchId)
}
}, [])

if (loading) return <>位置情報読込中</>

return (
<GeolocationContext.Provider value={{ currentLocation }}>
<div
style={{ display: 'flex', flexDirection: 'column', marginBottom: 10 }}>
<div>lat:{currentLocation?.latitude || 0}</div>
<div>lng:{currentLocation?.longitude || 0}</div>
<div>acc:{currentLocation?.accuracy || 0}</div>
<div>alt:{currentLocation?.altitude || 0}</div>
<div>error:{error}</div>
</div>
{children}
</GeolocationContext.Provider>
)
}

export const useWatchLocation = () => useContext(GeolocationContext)

0 comments on commit 563b688

Please sign in to comment.