Customizing the Traccar Tracking App (Part 2): Adding Dynamic Report Frequency
Last month I added a Fused Provider to the Traccar Android client . After using it for a while, one remaining inconvenience was that the report frequency is fixed. If I set it short, then while I’m sitting still at home or in the office, it keeps reporting almost the same position, wasting battery and network data and filling the database with duplicate points. If I set it long, the points end up several kilometers apart when driving, so the track turns into a series of straight lines.
Ideally it should report faster when moving and slower when stationary, so I kept customizing the client and added a dynamic report frequency feature.
Positioning Method
Building on my previous version, this time I use GPS_PROVIDER and NETWORK_PROVIDER together. Both providers stay on, each continuously fetching and caching its latest position, and a timer reads from the cache: GPS is preferred, and network is only used when GPS has no new position. In my testing this is more accurate than FUSED_PROVIDER, especially for high-frequency reporting while moving outdoors. The downside is that keeping the GPS module on all the time uses more battery, so it’s a trade-off.
Detecting Movement
The most direct way is to look at the speed the provider returns with each position, but it isn’t reliable: network positions usually have no speed, and GPS speed jumps around indoors or when stationary.
My idea was to look at how scattered the recent positions are. When stationary, positions only jitter within a small area; when moving, they spread out. Here’s how I approached it:
- At first I planned to cache all positions within a sliding time window and compute their variance every 5 seconds, with the report frequency proportional to the variance: high variance means a high frequency and short interval, low variance means a low frequency and long interval.
- After thinking it over, I optimized it into an Exponentially Weighted Moving Variance (EWMV). There’s no need to keep a position history; each new position just updates the mean and variance, both space and time complexity are O(1), and newer positions carry more weight.
The EWMV Algorithm
The latitude and longitude from Android’s location providers use the international WGS84 standard, which models the Earth as a slightly flattened ellipsoid, so I first have to project them into planar coordinates in meters before computing the variance. After the service starts, the first position received is used as the reference point; x is the distance east and y is the distance north. The longitude direction is multiplied by cos(latitude), because meridians get closer together toward the poles. In the conversion I approximate the ellipsoid as a sphere, counting every degree as 111,320 m (METERS_PER_DEGREE in the code); the error is within 1% and can be ignored. Then the mean and variance of x and y are updated separately, and finally combined into a dispersion (d for short below) = sqrt(varX + varY), in meters. The core code is as follows:
val x = (location.longitude - referenceLongitude) * METERS_PER_DEGREE * cos(latitudeRadians)
val y = (location.latitude - referenceLatitude) * METERS_PER_DEGREE
val diffX = x - meanX
meanX += ewmvAlpha * diffX
varianceX = (1 - ewmvAlpha) * (varianceX + ewmvAlpha * diffX * diffX)
val diffY = y - meanY
meanY += ewmvAlpha * diffY
varianceY = (1 - ewmvAlpha) * (varianceY + ewmvAlpha * diffY * diffY)
val dispersion = sqrt(varianceX + varianceY)
Two details are worth mentioning:
- Currently the reference point is simply the first position after the service starts, and it never changes afterwards. Where the reference point is doesn’t affect the variance calculation itself; the catch is that cos also keeps using the reference point’s latitude. Near the reference point that’s fine, with less than 1% error within 100 km. But if the service keeps running over a few hundred kilometers, the current latitude drifts far from the reference latitude and east-west distances get skewed. For example, after going 500 km north, east-west distances are overestimated by a bit over 3%; the error is larger at higher latitudes, and d is skewed along with it. Next time I’ll upgrade it to a dynamically updated reference point, shifting the reference point and the mean coordinates along with new positions, so no matter how far it travels, the error stays as small as it is near the reference point.
- Multiplying the longitude direction by cos(latitude) is really a local planar approximation (equirectangular projection). In reality the latitude changes as I move, so cos(latitude) keeps changing slightly, and strictly speaking converting everything with a single cos(latitude) introduces a small error. Setting aside the fixed reference point issue from point 1, if I only look at the differences between positions over the last few tens of seconds, which are at most a kilometer or two apart, the change in cos(latitude) over such a small range is negligible, and multiplying by cos(latitude) directly is what people usually do.
The computed mean and variance are only used to decide whether the device is moving. What gets reported is still the latest real position, not this mean.
α is the smoothing factor (ewmvAlpha in the code). A larger α weights new positions more and reacts faster; a smaller α is smoother. I currently use α = 20%. With one position every 5 seconds, the half-life works out to about 15 seconds (ln0.5/ln0.8 ≈ 3.1 samples), and a position from 1 minute ago has only about 7% weight left, so the dispersion roughly reflects movement over the last twenty-odd seconds.
At constant speed, the dispersion settles at sqrt(1-α)/α × the distance moved between samples. With my current settings that’s about 22 times the speed (in m/s), so d = 15 m corresponds to about 0.7 m/s, and d = 100 m to about 4.5 m/s.
Converting to Report Interval
When d is below 15 m, the slowest interval (300 seconds) is used; above 100 m, the fastest interval (10 seconds) is used; in between, it transitions smoothly.
At first I linearly interpolated the interval, but later found it wasn’t right: between 15 and 100 m the interval is generally too long, and only gets short near 100 m. For example, walking at 1.5 m/s gives a d of about 34 m, but the interval is still over 230 seconds, so two points end up more than 350 m apart.
So I switched to linearly interpolating the frequency (1/interval). Frequency is proportional to d, and d is proportional to speed, so the distance between two reported points stays roughly constant: about 45 to 60 m between 1.5 and 4.5 m/s, with no big gaps at moderate speeds.
val minFrequency = 1.0 / maxReportInterval
val maxFrequency = 1.0 / minReportInterval
val frequency = minFrequency + fraction * (maxFrequency - minFrequency)
return (1.0 / frequency).toLong()

Details
- At startup it assumes the device is moving: the variance starts at 100 squared, so it begins at the fastest interval. If the device is actually stationary, d drops below 15 m within about a minute and a half and it switches to the slowest interval. While stationary the interval grows faster than time passes, so this only causes 1 extra report.
- Each position is only used in the calculation once. The position’s timestamp is used as a marker, and only positions newer than the last one are taken; if no new position arrives within 5 seconds, the previous d is reused. Otherwise the same position would be counted repeatedly, pushing the variance down so the device is mistaken as stationary.
- Each position is only reported once. Reporting uses a separate timestamp marker: when it’s time, the newest position that hasn’t been reported yet is sent, and nothing is sent if there’s no new position. Whether the interval grows or shrinks while waiting, nothing is missed or reported twice.
- GPS is preferred. When picking a position, it first checks whether GPS has a new one; if so GPS is used, otherwise network.
Simulation
To make tuning easier, I wrote a simulation in Python using the same algorithm: one GPS position every 5 seconds with about 3 m of random error, simulating 3 minutes stationary, 4 minutes walking, 5 minutes driving, then 7 minutes stationary:

While stationary it only reported 2 points at startup. When walking, it started reporting after about 35 seconds, then roughly every 50 seconds. When driving it reported every 10 seconds. After stopping, it kept reporting every 10 seconds for about a minute, then gradually stretched to once every 300 seconds, so the stop location is also recorded accurately.
Results
I compiled it and tested it on my phone. In the log, age is how long ago the position was obtained when it was reported. Below is the log while driving: pulling away from a traffic light, d climbed from 48 m all the way to over 190 m and the report interval (i in the log) quickly dropped from 24.5 seconds to the fastest 10 seconds. Then, stopping at a red light, d gradually came down and i stretched to 19 seconds.

Here’s the log while stationary in the evening. Since there’s no GPS signal indoors, it automatically used network positioning, with acc (accuracy) showing 30 m. But the coordinates barely changed while stationary, so d is only 0 to just over 1 m, i stays at the slowest 300 seconds, and it only reports once every 5 minutes.

It works as expected: the track is much denser when driving, and it no longer keeps reporting duplicate points when stationary.
Slow Location Updates When the Phone Is Stationary
The stationary log also shows a problem: age is always around 16 to 20 seconds, so positions update quite slowly. Checking with debug logging (tested in a spot with GPS reception), I found that when the phone is stationary, the GPS and network providers only deliver a new position about every 20 seconds, much slower than the requested 5 seconds.
The reason is that the interval passed to requestLocationUpdates() only limits how fast the system delivers updates to the callback; it isn’t a guarantee, and GPS also throttles itself to save power when stationary or indoors. Moving isn’t really affected: in the driving log age is only 1 to 4 seconds, so GPS delivers new positions at roughly the requested interval.
The d calculation itself isn’t affected; when there’s no new position it just reuses the previous value. But EWMV decays per position, so when positions arrive slowly, the half-life in seconds gets longer and it reacts more slowly. For example, at startup d drops from 100 m to below 15 m in about a minute and a half with a new position every 5 seconds, but takes over 5 minutes with one every 20 seconds, with seven or eight extra reports along the way. I’ll look into dynamically adjusting α based on the actual time between positions later (α is currently a fixed setting), so the half-life stays a fixed amount of time regardless of how fast positions arrive.