fix(map): show raw coords as fallback when geocode fails or times out
- 5s timeout on Nominatim fetch (AbortSignal.timeout) - On network error, non-200, or empty result: show 'lat, lon' (5 decimal places) - Applies to both dashboard live location and history scrubber Co-authored-by: Copilot <[email protected]>
This commit is contained in:
@@ -31,19 +31,21 @@
|
|||||||
const key = `${lat.toFixed(3)},${lon.toFixed(3)}`;
|
const key = `${lat.toFixed(3)},${lon.toFixed(3)}`;
|
||||||
if (key === _lastGeoKey) return;
|
if (key === _lastGeoKey) return;
|
||||||
_lastGeoKey = key;
|
_lastGeoKey = key;
|
||||||
|
const fallback = `${lat.toFixed(5)}, ${lon.toFixed(5)}`;
|
||||||
fetch(`https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=${lat}&lon=${lon}`, {
|
fetch(`https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=${lat}&lon=${lon}`, {
|
||||||
headers: { 'Accept-Language': 'en' }
|
headers: { 'Accept-Language': 'en' },
|
||||||
|
signal: AbortSignal.timeout(5000),
|
||||||
})
|
})
|
||||||
.then(r => r.ok ? r.json() : null)
|
.then(r => r.ok ? r.json() : null)
|
||||||
.then(data => {
|
.then(data => {
|
||||||
if (!data) return;
|
if (!data) { replayLocation.textContent = fallback; return; }
|
||||||
const a = data.address || {};
|
const a = data.address || {};
|
||||||
const road = a.road || a.hamlet || a.village || a.suburb || '';
|
const road = a.road || a.hamlet || a.village || a.suburb || '';
|
||||||
const city = a.city || a.town || a.village || a.municipality || '';
|
const city = a.city || a.town || a.village || a.municipality || '';
|
||||||
replayLocation.textContent = [road, city].filter(Boolean).join(', ')
|
replayLocation.textContent = [road, city].filter(Boolean).join(', ')
|
||||||
|| data.display_name?.split(',').slice(0, 2).join(',') || '';
|
|| data.display_name?.split(',').slice(0, 2).join(',') || fallback;
|
||||||
})
|
})
|
||||||
.catch(() => {});
|
.catch(() => { replayLocation.textContent = fallback; });
|
||||||
}
|
}
|
||||||
|
|
||||||
function scheduleGeocode(lat, lon) {
|
function scheduleGeocode(lat, lon) {
|
||||||
|
|||||||
+7
-4
@@ -196,19 +196,22 @@ _recenterBtn.addEventListener('click', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
function reverseGeocode(lat, lon) {
|
function reverseGeocode(lat, lon) {
|
||||||
|
const fallback = `${lat.toFixed(5)}, ${lon.toFixed(5)}`;
|
||||||
|
const el = document.getElementById('gps-location-name');
|
||||||
fetch(`https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=${lat}&lon=${lon}`, {
|
fetch(`https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=${lat}&lon=${lon}`, {
|
||||||
headers: { 'Accept-Language': 'en' }
|
headers: { 'Accept-Language': 'en' },
|
||||||
|
signal: AbortSignal.timeout(5000),
|
||||||
})
|
})
|
||||||
.then(r => r.ok ? r.json() : null)
|
.then(r => r.ok ? r.json() : null)
|
||||||
.then(data => {
|
.then(data => {
|
||||||
if (!data) return;
|
if (!data) { el.textContent = fallback; return; }
|
||||||
const a = data.address || {};
|
const a = data.address || {};
|
||||||
const name = a.road || a.hamlet || a.village || a.suburb || '';
|
const name = a.road || a.hamlet || a.village || a.suburb || '';
|
||||||
const city = a.city || a.town || a.village || a.municipality || '';
|
const city = a.city || a.town || a.village || a.municipality || '';
|
||||||
const label = [name, city].filter(Boolean).join(', ');
|
const label = [name, city].filter(Boolean).join(', ');
|
||||||
document.getElementById('gps-location-name').textContent = label || data.display_name?.split(',').slice(0,2).join(',') || '';
|
el.textContent = label || data.display_name?.split(',').slice(0, 2).join(',') || fallback;
|
||||||
})
|
})
|
||||||
.catch(() => {});
|
.catch(() => { el.textContent = fallback; });
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.on('gps_location', function(data) {
|
socket.on('gps_location', function(data) {
|
||||||
|
|||||||
Reference in New Issue
Block a user