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