forked from streamich/react-use
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
103 additions
and
10 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
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
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,20 @@ | ||
# `useGeolocation` | ||
|
||
React sensor hook that tracks user's geographic location. | ||
|
||
|
||
## Usage | ||
|
||
```jsx | ||
import {useGeolocation} from 'react-use'; | ||
|
||
const Demo = () => { | ||
const state = useGeolocation(); | ||
|
||
return ( | ||
<pre> | ||
{JSON.stringify(state, null, 2)} | ||
</pre> | ||
); | ||
}; | ||
``` |
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
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,18 @@ | ||
import * as React from 'react'; | ||
import {storiesOf} from '@storybook/react'; | ||
import {useGeolocation} from '..'; | ||
|
||
const Demo = () => { | ||
const state = useGeolocation(); | ||
|
||
return ( | ||
<pre> | ||
{JSON.stringify(state, null, 2)} | ||
</pre> | ||
); | ||
}; | ||
|
||
storiesOf('useGeolocation', module) | ||
.add('Example', () => | ||
<Demo/> | ||
) |
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
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,56 @@ | ||
import {useState, useEffect} from './react'; | ||
|
||
export interface GeoLocationSensorState { | ||
accuracy: number, | ||
altitude: number, | ||
altitudeAccuracy: number, | ||
heading: number, | ||
latitude: number, | ||
longitude: number, | ||
speed: number, | ||
timestamp: number | ||
} | ||
|
||
const useGeolocation = () => { | ||
const [state, setState] = useState({ | ||
accuracy: null, | ||
altitude: null, | ||
altitudeAccuracy: null, | ||
heading: null, | ||
latitude: null, | ||
longitude: null, | ||
speed: null, | ||
timestamp: Date.now(), | ||
}); | ||
let mounted = true; | ||
let watchId: any; | ||
|
||
const onEvent = (event: any) => { | ||
if (mounted) { | ||
setState({ | ||
accuracy: event.coords.accuracy, | ||
altitude: event.coords.altitude, | ||
altitudeAccuracy: event.coords.altitudeAccuracy, | ||
heading: event.coords.heading, | ||
latitude: event.coords.latitude, | ||
longitude: event.coords.longitude, | ||
speed: event.coords.speed, | ||
timestamp: event.timestamp, | ||
}); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
navigator.geolocation.getCurrentPosition(onEvent); | ||
watchId = navigator.geolocation.watchPosition(onEvent); | ||
|
||
return () => { | ||
mounted = false; | ||
navigator.geolocation.clearWatch(watchId); | ||
}; | ||
}, [0]); | ||
|
||
return state; | ||
}; | ||
|
||
export default useGeolocation; |