fix(geocode): accurate street name + always shows on dashboard

- Cache key uses 4 decimal places (~11m) instead of 3 (~111m) for correct street
- Dashboard geocodes on first GPS fix, then again every >50m moved (throttled 15s)
- Removed stale console.log

Co-authored-by: Copilot <[email protected]>
This commit is contained in:
Mathieu Moran
2026-08-10 03:21:37 -04:00
co-authored by Copilot
parent c188fb9d14
commit 11f07dec38
2 changed files with 17 additions and 9 deletions
+13 -5
View File
@@ -186,6 +186,7 @@ socket.on('obd_error', function (data) {
let mapCentered = false;
let _lastLat = null, _lastLon = null;
let _lastGeoLat = null, _lastGeoLon = null;
let _geocodeTimer = null;
const _recenterBtn = document.getElementById('btn-recenter');
@@ -198,14 +199,19 @@ _recenterBtn.addEventListener('click', function () {
function reverseGeocode(lat, lon) {
const el = document.getElementById('gps-location-name');
const fallback = `${lat.toFixed(5)}, ${lon.toFixed(5)}`;
fetch(`/api/geocode?lat=${lat}&lon=${lon}`, { signal: AbortSignal.timeout(4000) })
fetch(`/api/geocode?lat=${lat}&lon=${lon}`, { signal: AbortSignal.timeout(6000) })
.then(r => r.ok ? r.json() : null)
.then(data => { el.textContent = (data && data.label) ? data.label : fallback; })
.catch(() => { el.textContent = fallback; });
}
function _movedEnough(lat, lon) {
if (_lastGeoLat === null) return true;
// ~0.0005 deg ≈ 50m
return Math.abs(lat - _lastGeoLat) > 0.0005 || Math.abs(lon - _lastGeoLon) > 0.0005;
}
socket.on('gps_location', function(data) {
console.log("location received")
const LocationX = document.getElementById('gpsLocationX');
const LocationY = document.getElementById('gpsLocationY');
LocationX.innerHTML = data[0];
@@ -225,10 +231,12 @@ socket.on('gps_location', function(data) {
_lastLat = lat;
_lastLon = lon;
_recenterBtn.hidden = false;
// Throttle geocode to at most once every 30s
if (!_geocodeTimer) {
// Geocode when moved >50m, throttled to at most once every 15s
if (!_geocodeTimer && _movedEnough(lat, lon)) {
_lastGeoLat = lat;
_lastGeoLon = lon;
reverseGeocode(lat, lon);
_geocodeTimer = setTimeout(() => { _geocodeTimer = null; }, 30000);
_geocodeTimer = setTimeout(() => { _geocodeTimer = null; }, 15000);
}
}
});