1+ import { GeoEmaSmoother } from "./geo_smoothing" ;
12import { GeoListenMode } from "./mixer" ;
23import { logger } from "./shims" ;
34import { Coordinates , GeoPositionOptions } from "./types" ;
45
56const initialGeoTimeoutSeconds = 6 ;
7+ // default; can be overridden by options on Roundware instantiation
68const geoUpdateThrottleMs = 0 ;
79
810const frameworkDefaultCoords : Coordinates = {
@@ -44,6 +46,7 @@ export class GeoPosition {
4446 updateCallback : CallableFunction ;
4547 private _geoWatchID ?: number | null ;
4648 private _geoPositionStatus : true | number = 3 ;
49+ private _smoother ?: GeoEmaSmoother ;
4750
4851 constructor ( navigator : Window [ `navigator`] , options : GeoPositionOptions ) {
4952 this . _navigator = navigator ;
@@ -63,6 +66,24 @@ export class GeoPosition {
6366 this . updateCallback = ( ) => { } ;
6467 this . _geoWatchID = null ;
6568
69+ // Setup optional smoother
70+ const {
71+ geoSmoothingEnabled = false ,
72+ geoSmoothingAlpha = 0.15 ,
73+ geoSmoothingMinAccuracyMeters = 20 ,
74+ geoSmoothingMinEmitDeltaMeters = 0 ,
75+ geoSmoothingResetJumpMeters = 50 ,
76+ } = options ;
77+ if ( geoSmoothingEnabled ) {
78+ this . _smoother = new GeoEmaSmoother ( {
79+ enabled : geoSmoothingEnabled ,
80+ alpha : geoSmoothingAlpha ,
81+ minAccuracyMeters : geoSmoothingMinAccuracyMeters ,
82+ minEmitDeltaMeters : geoSmoothingMinEmitDeltaMeters ,
83+ resetJumpMeters : geoSmoothingResetJumpMeters ,
84+ } ) ;
85+ }
86+
6687 //console.info({ defaultCoords: this.defaultCoords });
6788 }
6889
@@ -174,11 +195,29 @@ export class GeoPosition {
174195 ) } `
175196 ) ;
176197
198+ // Optionally smooth before forwarding; always emit as simple Coordinates
199+ let coordsToSend : Coordinates = {
200+ latitude : coords . latitude ,
201+ longitude : coords . longitude ,
202+ } ;
203+ if ( this . _smoother ) {
204+ const smoothed = this . _smoother . update ( updatedPosition ) ;
205+ if ( smoothed ) {
206+ coordsToSend = {
207+ latitude : smoothed . latitude ,
208+ longitude : smoothed . longitude ,
209+ } ;
210+ } else {
211+ // rejected by smoothing (poor accuracy or within deadband)
212+ return ;
213+ }
214+ }
215+
177216 this . _lastUpdateTime = now ;
178- this . _lastCoords = coords ;
217+ this . _lastCoords = coordsToSend ;
179218 this . _geoPositionStatus = true ;
180- logger . info ( "Received updated geolocation:" , coords ) ;
181- this . updateCallback ( coords ) ;
219+ logger . info ( "Received updated geolocation:" , coordsToSend ) ;
220+ this . updateCallback ( coordsToSend ) ;
182221 } ,
183222 ( error ) => {
184223 logger . warn (
0 commit comments