diff --git a/src/components/map/GpxRecord.tsx b/src/components/map/GpxRecord.tsx index 5bdc77f..704d9c8 100644 --- a/src/components/map/GpxRecord.tsx +++ b/src/components/map/GpxRecord.tsx @@ -23,8 +23,10 @@ import { startBackgroundGeolocation, stopBackgroundGeolocation, } from '../../lib/background-geolocation'; -import { deleteCurrent, saveCurrent } from '../../db/gpx'; +import { appendTrkpt, deleteCurrent, saveCurrent } from '../../db/gpx'; import { enterAnimation, leaveAnimation } from '../../lib/animation'; +import { useDispatch } from 'react-redux'; +import { mapActions } from '../../store/map'; const GpxRecord: React.FC<{}> = () => { const db = useDB(); @@ -54,8 +56,31 @@ const GpxRecord: React.FC<{}> = () => { modal.current?.dismiss(); }; + 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, + }) + ); + }; + const startRecording = () => { - startBackgroundGeolocation(db).then((result) => { + startBackgroundGeolocation(newLocationHandler).then((result) => { setWatcher_id(result); }); setIsRecording(true); @@ -80,7 +105,6 @@ const GpxRecord: React.FC<{}> = () => { pauseRecording(); }; - return ( diff --git a/src/lib/background-geolocation.ts b/src/lib/background-geolocation.ts index 9d25547..795bda4 100644 --- a/src/lib/background-geolocation.ts +++ b/src/lib/background-geolocation.ts @@ -1,6 +1,5 @@ import { BackgroundGeolocationPlugin } from '@capacitor-community/background-geolocation'; import { registerPlugin } from '@capacitor/core'; -import { appendTrkpt } from '../db/gpx'; const BackgroundGeolocation = registerPlugin( 'BackgroundGeolocation' @@ -35,7 +34,7 @@ const backgroundGeolocationConfig = { distanceFilter: 5, }; -export const startBackgroundGeolocation = async (db: any) => { +export const startBackgroundGeolocation = async (newLocationHandler: any) => { const locationHandler = (location: any, error: any) => { console.log('com.dyomedea.dyomedea LOG', ' - Callback'); if (error) { @@ -58,33 +57,8 @@ export const startBackgroundGeolocation = async (db: any) => { } console.log(location); if (location !== undefined) { - appendTrkpt(db, { - $: { - lat: location.latitude, - lon: location.longitude, - }, - ele: location.altitude, - time: new Date(location.time).toISOString(), - extensions: { - speed: location.speed, - accuracy: location.accuracy, - }, - }); - - //setCenter([location.latitude, location.longitude]); - //setPosition([location.latitude, location.longitude]); - // dispatch( - // gpxActions.appendTrkpt({ - // trkKey: 'current', - // trkpt: { - // $: { - // lat: location.latitude, - // lon: location.longitude, - // }, - // }, - // }) - // ); - } + newLocationHandler(location); + } return console.log('com.dyomedea.dyomedea LOG', ' - location: ', location); };