[d565449] | 1 | import { __assign } from "tslib";
|
---|
| 2 | import { useEffect, useState } from 'react';
|
---|
| 3 | var useGeolocation = function (options) {
|
---|
| 4 | var _a = useState({
|
---|
| 5 | loading: true,
|
---|
| 6 | accuracy: null,
|
---|
| 7 | altitude: null,
|
---|
| 8 | altitudeAccuracy: null,
|
---|
| 9 | heading: null,
|
---|
| 10 | latitude: null,
|
---|
| 11 | longitude: null,
|
---|
| 12 | speed: null,
|
---|
| 13 | timestamp: Date.now(),
|
---|
| 14 | }), state = _a[0], setState = _a[1];
|
---|
| 15 | var mounted = true;
|
---|
| 16 | var watchId;
|
---|
| 17 | var onEvent = function (event) {
|
---|
| 18 | if (mounted) {
|
---|
| 19 | setState({
|
---|
| 20 | loading: false,
|
---|
| 21 | accuracy: event.coords.accuracy,
|
---|
| 22 | altitude: event.coords.altitude,
|
---|
| 23 | altitudeAccuracy: event.coords.altitudeAccuracy,
|
---|
| 24 | heading: event.coords.heading,
|
---|
| 25 | latitude: event.coords.latitude,
|
---|
| 26 | longitude: event.coords.longitude,
|
---|
| 27 | speed: event.coords.speed,
|
---|
| 28 | timestamp: event.timestamp,
|
---|
| 29 | });
|
---|
| 30 | }
|
---|
| 31 | };
|
---|
| 32 | var onEventError = function (error) {
|
---|
| 33 | return mounted && setState(function (oldState) { return (__assign(__assign({}, oldState), { loading: false, error: error })); });
|
---|
| 34 | };
|
---|
| 35 | useEffect(function () {
|
---|
| 36 | navigator.geolocation.getCurrentPosition(onEvent, onEventError, options);
|
---|
| 37 | watchId = navigator.geolocation.watchPosition(onEvent, onEventError, options);
|
---|
| 38 | return function () {
|
---|
| 39 | mounted = false;
|
---|
| 40 | navigator.geolocation.clearWatch(watchId);
|
---|
| 41 | };
|
---|
| 42 | }, []);
|
---|
| 43 | return state;
|
---|
| 44 | };
|
---|
| 45 | export default useGeolocation;
|
---|