From 11c2a1713f407d5fe097f834bbcadf9bb08133fd Mon Sep 17 00:00:00 2001 From: evlist Date: Sun, 2 Oct 2022 21:52:02 +0200 Subject: [PATCH] Firstattempt to reduce te number of points when recording a track (#13): the minimal distance is now 10m and a filter with a minimal time of 15s has been added. This should now be tested on the road ! --- src/components/map/GpxRecord.tsx | 54 ++++++++++++++++++++----------- src/lib/background-geolocation.ts | 2 +- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/components/map/GpxRecord.tsx b/src/components/map/GpxRecord.tsx index 704d9c8..9953705 100644 --- a/src/components/map/GpxRecord.tsx +++ b/src/components/map/GpxRecord.tsx @@ -1,4 +1,4 @@ -import React, { Fragment, useRef, useState } from 'react'; +import React, { Fragment, useEffect, useRef, useState } from 'react'; import { useDB, useFind } from 'react-pouchdb'; @@ -28,6 +28,12 @@ import { enterAnimation, leaveAnimation } from '../../lib/animation'; import { useDispatch } from 'react-redux'; import { mapActions } from '../../store/map'; +const MIN_TIME_BETWEEN_LOCATIONS = 15 * 1000; + +declare global { + var $lastValidLocationTime: number; +} + const GpxRecord: React.FC<{}> = () => { const db = useDB(); @@ -59,27 +65,39 @@ const GpxRecord: React.FC<{}> = () => { const dispatch = useDispatch(); const newLocationHandler = (location: any) => { - appendTrkpt(db, { - $: { - lat: location.latitude, - lon: location.longitude, - }, - ele: location.altitude, - time: new Date(location.time).toISOString(), - extensions: { - speed: location.speed, - accuracy: location.accuracy, - }, - }); - dispatch( - mapActions.setCurrent({ - lat: location.latitude, - lon: location.longitude, - }) + console.log( + `Location filtering, elapsed time: ${ + location.time - globalThis.$lastValidLocationTime + }` ); + if ( + location.time - globalThis.$lastValidLocationTime > + MIN_TIME_BETWEEN_LOCATIONS + ) { + globalThis.$lastValidLocationTime = location.time; + appendTrkpt(db, { + $: { + lat: location.latitude, + lon: location.longitude, + }, + ele: location.altitude, + time: new Date(location.time).toISOString(), + extensions: { + speed: location.speed, + accuracy: location.accuracy, + }, + }); + dispatch( + mapActions.setCurrent({ + lat: location.latitude, + lon: location.longitude, + }) + ); + } }; const startRecording = () => { + globalThis.$lastValidLocationTime = 0; startBackgroundGeolocation(newLocationHandler).then((result) => { setWatcher_id(result); }); diff --git a/src/lib/background-geolocation.ts b/src/lib/background-geolocation.ts index 795bda4..052ccb5 100644 --- a/src/lib/background-geolocation.ts +++ b/src/lib/background-geolocation.ts @@ -31,7 +31,7 @@ const backgroundGeolocationConfig = { // The minimum number of metres between subsequent locations. Defaults // to 0. - distanceFilter: 5, + distanceFilter: 10, }; export const startBackgroundGeolocation = async (newLocationHandler: any) => {