Files
smartphone-n8n-tracking/index_owntrack.html
Joachim Hummel 40aecaafc0 Add extensive debugging to loadLocations function
- Add console.log statements at each step
- Show error message in status widget
- Add coordinate validation
- Remove incorrect success/failure logic

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-14 15:45:55 +00:00

208 lines
7.1 KiB
HTML

<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Location Test</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
<style>
body { margin: 0; padding: 0; font-family: Arial, sans-serif; }
#map { height: 100vh; width: 100%; }
.info {
position: absolute;
top: 10px;
right: 10px;
background: white;
padding: 15px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
z-index: 1000;
min-width: 200px;
}
.toggle-btn {
margin-top: 15px;
padding: 10px 15px;
border: none;
border-radius: 6px;
cursor: pointer;
font-weight: bold;
width: 100%;
transition: all 0.3s ease;
font-size: 14px;
}
.toggle-btn.active {
background: #4CAF50;
color: white;
}
.toggle-btn.inactive {
background: #f44336;
color: white;
}
.toggle-btn:hover {
opacity: 0.8;
}
.status-indicator {
display: inline-block;
width: 8px;
height: 8px;
border-radius: 50%;
margin-right: 6px;
}
.status-indicator.active {
background: #4CAF50;
animation: pulse 2s infinite;
}
.status-indicator.inactive {
background: #999;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
</style>
</head>
<body>
<div id="map"></div>
<div class="info">
<h3>📍 Location Tracker</h3>
<div id="status">Lade...</div>
<button id="toggleBtn" class="toggle-btn active" onclick="toggleAutoRefresh()">
<span class="status-indicator active"></span>
Auto-Refresh: AN
</button>
</div>
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<script>
// Karte initialisieren (München)
const map = L.map('map').setView([48.1351, 11.5820], 12);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap'
}).addTo(map);
// API URL - anpassen an deine Domain
const API_URL = 'https://n8n.unixweb.home64.de/webhook/location';
// Auto-Refresh State
let autoRefreshEnabled = true;
let refreshInterval = null;
let markers = [];
let polylines = [];
async function loadLocations() {
try {
console.log('Fetching locations from:', API_URL);
const response = await fetch(API_URL);
console.log('Response status:', response.status);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
console.log('Data received:', data);
// Alte Marker und Linien entfernen
markers.forEach(marker => map.removeLayer(marker));
polylines.forEach(polyline => map.removeLayer(polyline));
markers = [];
polylines = [];
// Status aktualisieren
const statusDiv = document.getElementById('status');
statusDiv.innerHTML =
`Punkte: ${data.total_points || 0}<br>` +
`Status: ✅ Verbunden`;
if (data.current) {
const loc = data.current;
console.log('Current location:', loc);
// Popup-Inhalt aufbauen
let popupContent = `${loc.marker_label}<br>${loc.display_time}`;
// Batterie hinzufügen, falls vorhanden
if (loc.battery !== undefined && loc.battery !== null) {
popupContent += `<br>🔋 Batterie: ${loc.battery}%`;
}
// Geschwindigkeit hinzufügen, falls vorhanden
if (loc.speed !== undefined && loc.speed !== null) {
const speedKmh = (loc.speed * 3.6).toFixed(1); // m/s zu km/h
popupContent += `<br>🚗 Speed: ${speedKmh} km/h`;
}
const lat = parseFloat(loc.latitude);
const lon = parseFloat(loc.longitude);
if (isNaN(lat) || isNaN(lon)) {
throw new Error(`Invalid coordinates: lat=${loc.latitude}, lon=${loc.longitude}`);
}
const marker = L.marker([lat, lon])
.addTo(map)
.bindPopup(popupContent)
.openPopup();
markers.push(marker);
map.setView([lat, lon], 15);
// Historie als Linie
if (data.history && data.history.length > 1) {
const coords = data.history.map(h => [parseFloat(h.latitude), parseFloat(h.longitude)]);
const polyline = L.polyline(coords, {color: 'blue', weight: 3}).addTo(map);
polylines.push(polyline);
}
} else {
console.log('No current location in response');
}
} catch (error) {
console.error('Load error:', error);
console.error('Error stack:', error.stack);
document.getElementById('status').innerHTML =
`❌ Verbindungsfehler<br><small>${error.message}</small>`;
}
}
function toggleAutoRefresh() {
autoRefreshEnabled = !autoRefreshEnabled;
const btn = document.getElementById('toggleBtn');
const indicator = btn.querySelector('.status-indicator');
if (autoRefreshEnabled) {
// Aktiviere Auto-Refresh
btn.className = 'toggle-btn active';
btn.innerHTML = '<span class="status-indicator active"></span>Auto-Refresh: AN';
startAutoRefresh();
} else {
// Deaktiviere Auto-Refresh
btn.className = 'toggle-btn inactive';
btn.innerHTML = '<span class="status-indicator inactive"></span>Auto-Refresh: AUS';
stopAutoRefresh();
}
}
function startAutoRefresh() {
if (refreshInterval) clearInterval(refreshInterval);
refreshInterval = setInterval(loadLocations, 5000);
}
function stopAutoRefresh() {
if (refreshInterval) {
clearInterval(refreshInterval);
refreshInterval = null;
}
}
// Initial laden
loadLocations();
// Auto-refresh starten
startAutoRefresh();
</script>
</body>
</html>