Umfassende Verbesserungen am HTML/CSS/JS-Code: HTML & Meta-Tags: - Erweiterte Meta-Tags für SEO (description, keywords, author) - Open Graph Tags für Social Media Sharing - Twitter Card Meta-Tags - Theme-color für mobile Geräte - Preconnect und DNS-Prefetch für externe Ressourcen - SRI-Hashes für Leaflet-Ressourcen Semantik & Accessibility: - Semantische HTML5-Elemente (<main>, <aside>, <time>) - ARIA-Labels und Rollen (role="application", role="complementary") - ARIA-Live-Regionen für dynamische Inhalte (aria-live="polite") - Verbesserte Strukturierung für Screenreader Performance: - defer für externe Scripts - Preconnect für kritische externe Domains - DNS-Prefetch für Tile-Server Code-Qualität: - Umfassende JSDoc-Kommentare für alle Funktionen - CSS-Abschnitte mit Kommentaren strukturiert - 'use strict' Modus aktiviert - Konstanten für magische Werte (BERLIN_CENTER, DEFAULT_ZOOM) Fehlerbehandlung: - Request Timeout (10 Sekunden) mit AbortController - Differenzierte Fehlerbehandlung (Timeout, Network, Generic) - Null-Checks für DOM-Elemente - Array.isArray() Validierung für API-Daten - Benutzerfreundliche Console-Logs Code-Organisation: - Logische Gruppierung mit Kommentar-Headern - Konsistente Formatierung - Verbesserte Lesbarkeit durch Inline-Kommentare - Bessere Variablennamen 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
895 lines
33 KiB
HTML
895 lines
33 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="de">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
<!-- Primary Meta Tags -->
|
|
<title>Berlin Live Transit - Echtzeit-Nahverkehrskarte</title>
|
|
<meta name="title" content="Berlin Live Transit - Echtzeit-Nahverkehrskarte">
|
|
<meta name="description" content="Verfolgen Sie den Berliner Nahverkehr in Echtzeit. U-Bahn, S-Bahn, Trams, Busse und Fähren live auf der Karte.">
|
|
<meta name="keywords" content="Berlin, Transit, Nahverkehr, Live, Karte, U-Bahn, S-Bahn, Tram, Bus, BVG, VBB">
|
|
<meta name="author" content="Berlin Live Transit">
|
|
<meta name="theme-color" content="#0a0e27">
|
|
|
|
<!-- Open Graph / Facebook -->
|
|
<meta property="og:type" content="website">
|
|
<meta property="og:url" content="https://example.com/">
|
|
<meta property="og:title" content="Berlin Live Transit - Echtzeit-Nahverkehrskarte">
|
|
<meta property="og:description" content="Verfolgen Sie den Berliner Nahverkehr in Echtzeit. U-Bahn, S-Bahn, Trams, Busse und Fähren live auf der Karte.">
|
|
|
|
<!-- Twitter -->
|
|
<meta property="twitter:card" content="summary_large_image">
|
|
<meta property="twitter:url" content="https://example.com/">
|
|
<meta property="twitter:title" content="Berlin Live Transit - Echtzeit-Nahverkehrskarte">
|
|
<meta property="twitter:description" content="Verfolgen Sie den Berliner Nahverkehr in Echtzeit. U-Bahn, S-Bahn, Trams, Busse und Fähren live auf der Karte.">
|
|
|
|
<!-- Preconnect to external resources for better performance -->
|
|
<link rel="preconnect" href="https://unpkg.com" crossorigin>
|
|
<link rel="preconnect" href="https://v6.vbb.transport.rest" crossorigin>
|
|
<link rel="dns-prefetch" href="https://basemaps.cartocdn.com">
|
|
<style>
|
|
/* ===== Base Styles ===== */
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
body {
|
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
|
height: 100vh;
|
|
overflow: hidden;
|
|
position: relative;
|
|
background: #0a0e27;
|
|
cursor: none;
|
|
}
|
|
|
|
/* ===== Map Container ===== */
|
|
#map {
|
|
position: absolute;
|
|
top: 0;
|
|
left: 0;
|
|
width: 100%;
|
|
height: 100%;
|
|
z-index: 0;
|
|
transition: opacity 0.5s ease;
|
|
cursor: none !important;
|
|
}
|
|
|
|
/* ===== Clock Display ===== */
|
|
.clock-container {
|
|
position: absolute;
|
|
bottom: 40px;
|
|
right: 40px;
|
|
z-index: 1000;
|
|
background: rgba(20, 20, 30, 0.85);
|
|
backdrop-filter: blur(20px);
|
|
padding: 35px 45px;
|
|
border-radius: 25px;
|
|
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.6);
|
|
border: 1px solid rgba(255, 255, 255, 0.15);
|
|
transition: background 0.3s ease, box-shadow 0.3s ease;
|
|
min-width: 350px;
|
|
min-height: 160px;
|
|
display: flex;
|
|
flex-direction: column;
|
|
justify-content: center;
|
|
}
|
|
|
|
.clock-container:hover {
|
|
background: rgba(20, 20, 30, 0.95);
|
|
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.7);
|
|
transform: none;
|
|
}
|
|
|
|
.time {
|
|
font-size: 84px;
|
|
font-weight: 200;
|
|
color: rgba(255, 255, 255, 0.98);
|
|
letter-spacing: -2px;
|
|
line-height: 1;
|
|
text-shadow: 0 3px 20px rgba(0, 0, 0, 0.8);
|
|
}
|
|
|
|
.date {
|
|
font-size: 18px;
|
|
color: rgba(255, 255, 255, 0.75);
|
|
margin-top: 8px;
|
|
font-weight: 400;
|
|
text-shadow: 0 2px 10px rgba(0, 0, 0, 0.7);
|
|
}
|
|
|
|
/* ===== Loading Indicator ===== */
|
|
.loading-indicator {
|
|
position: absolute;
|
|
bottom: 40px;
|
|
left: 50%;
|
|
transform: translateX(-50%);
|
|
z-index: 1000;
|
|
background: rgba(20, 20, 30, 0.9);
|
|
backdrop-filter: blur(15px);
|
|
padding: 15px 30px;
|
|
border-radius: 30px;
|
|
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
|
|
border: 1px solid rgba(255, 255, 255, 0.15);
|
|
opacity: 0;
|
|
transition: opacity 0.3s ease;
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 12px;
|
|
}
|
|
|
|
.loading-indicator.show {
|
|
opacity: 1;
|
|
}
|
|
|
|
.spinner {
|
|
width: 16px;
|
|
height: 16px;
|
|
border: 2px solid rgba(255, 255, 255, 0.3);
|
|
border-top-color: #00ffff;
|
|
border-radius: 50%;
|
|
animation: spin 0.8s linear infinite;
|
|
}
|
|
|
|
@keyframes spin {
|
|
to { transform: rotate(360deg); }
|
|
}
|
|
|
|
.loading-text {
|
|
color: rgba(255, 255, 255, 0.9);
|
|
font-size: 14px;
|
|
font-weight: 500;
|
|
}
|
|
|
|
/* ===== Vehicle Markers ===== */
|
|
.vehicle-marker-container {
|
|
position: relative;
|
|
width: 32px;
|
|
height: 32px;
|
|
}
|
|
|
|
.vehicle-marker {
|
|
width: 32px;
|
|
height: 32px;
|
|
position: absolute;
|
|
top: 50%;
|
|
left: 50%;
|
|
transform: translate(-50%, -50%);
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
font-size: 20px;
|
|
font-weight: 900;
|
|
animation: vehicle-pulse 2s ease-in-out infinite;
|
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
|
text-shadow:
|
|
0 0 8px currentColor,
|
|
0 0 15px currentColor,
|
|
0 2px 4px rgba(0, 0, 0, 0.8);
|
|
cursor: pointer;
|
|
transition: transform 0.2s ease;
|
|
filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.6));
|
|
background: transparent !important;
|
|
}
|
|
|
|
.vehicle-marker:hover {
|
|
transform: translate(-50%, -50%) scale(1.2);
|
|
}
|
|
|
|
@keyframes vehicle-pulse {
|
|
0%, 100% {
|
|
transform: translate(-50%, -50%) scale(1);
|
|
}
|
|
50% {
|
|
transform: translate(-50%, -50%) scale(1.15);
|
|
}
|
|
}
|
|
|
|
@keyframes vehicle-ripple {
|
|
0% {
|
|
transform: translate(-50%, -50%) scale(0.6);
|
|
opacity: 0.8;
|
|
}
|
|
100% {
|
|
transform: translate(-50%, -50%) scale(2);
|
|
opacity: 0;
|
|
}
|
|
}
|
|
|
|
.leaflet-marker-icon {
|
|
transition: transform 10s linear !important;
|
|
}
|
|
|
|
.vehicle-label {
|
|
position: absolute;
|
|
top: -26px;
|
|
left: 50%;
|
|
transform: translateX(-50%);
|
|
background: rgba(0, 0, 0, 0.95);
|
|
color: white;
|
|
padding: 4px 10px;
|
|
border-radius: 10px;
|
|
font-size: 12px;
|
|
font-weight: 700;
|
|
white-space: nowrap;
|
|
box-shadow: 0 3px 10px rgba(0, 0, 0, 0.6);
|
|
border: 2px solid rgba(255, 255, 255, 0.3);
|
|
letter-spacing: 0.5px;
|
|
pointer-events: none;
|
|
}
|
|
|
|
/* ===== Vehicle Type Colors ===== */
|
|
.vehicle-subway {
|
|
background: #0066cc;
|
|
color: #0066cc;
|
|
}
|
|
.vehicle-suburban {
|
|
background: #009933;
|
|
color: #009933;
|
|
}
|
|
.vehicle-tram {
|
|
background: #cc0000;
|
|
color: #cc0000;
|
|
}
|
|
.vehicle-bus {
|
|
background: #993399;
|
|
color: #993399;
|
|
}
|
|
.vehicle-ferry {
|
|
background: #0099cc;
|
|
color: #0099cc;
|
|
}
|
|
.vehicle-regional {
|
|
background: #cc6600;
|
|
color: #cc6600;
|
|
}
|
|
|
|
/* ===== User Location Marker ===== */
|
|
.user-location-marker {
|
|
position: relative;
|
|
width: 40px;
|
|
height: 40px;
|
|
}
|
|
|
|
.user-location-dot {
|
|
position: absolute;
|
|
width: 18px;
|
|
height: 18px;
|
|
background: #00ffff;
|
|
border: 4px solid white;
|
|
border-radius: 50%;
|
|
top: 50%;
|
|
left: 50%;
|
|
transform: translate(-50%, -50%);
|
|
box-shadow:
|
|
0 0 20px rgba(0, 255, 255, 0.8),
|
|
0 0 40px rgba(0, 255, 255, 0.6);
|
|
z-index: 2;
|
|
}
|
|
|
|
.user-location-pulse {
|
|
position: absolute;
|
|
width: 40px;
|
|
height: 40px;
|
|
border: 3px solid #00ffff;
|
|
border-radius: 50%;
|
|
top: 50%;
|
|
left: 50%;
|
|
transform: translate(-50%, -50%);
|
|
animation: location-pulse 2s ease-out infinite;
|
|
z-index: 1;
|
|
}
|
|
|
|
@keyframes location-pulse {
|
|
0% {
|
|
transform: translate(-50%, -50%) scale(0.5);
|
|
opacity: 1;
|
|
}
|
|
100% {
|
|
transform: translate(-50%, -50%) scale(2);
|
|
opacity: 0;
|
|
}
|
|
}
|
|
|
|
/* ===== Leaflet Popup Customization ===== */
|
|
.leaflet-popup-content-wrapper {
|
|
background: rgba(20, 20, 30, 0.98);
|
|
backdrop-filter: blur(15px);
|
|
color: white;
|
|
border-radius: 15px;
|
|
border: 1px solid rgba(255, 255, 255, 0.2);
|
|
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.6);
|
|
}
|
|
|
|
.leaflet-popup-content {
|
|
margin: 18px;
|
|
font-size: 14px;
|
|
}
|
|
|
|
.leaflet-popup-tip {
|
|
background: rgba(20, 20, 30, 0.98);
|
|
}
|
|
|
|
/* ===== Responsive Design ===== */
|
|
@media (max-width: 768px) {
|
|
.clock-container {
|
|
padding: 25px 30px;
|
|
bottom: 20px;
|
|
right: 20px;
|
|
}
|
|
|
|
.time {
|
|
font-size: 56px;
|
|
}
|
|
|
|
.date {
|
|
font-size: 14px;
|
|
}
|
|
}
|
|
|
|
/* ===== Cursor Styles ===== */
|
|
* {
|
|
cursor: none !important;
|
|
}
|
|
</style>
|
|
|
|
<!-- External Resources -->
|
|
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
|
|
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
|
|
crossorigin="" />
|
|
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
|
|
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
|
|
crossorigin=""
|
|
defer></script>
|
|
</head>
|
|
<body>
|
|
<!-- Main Application Container -->
|
|
<main id="app" role="main">
|
|
<!-- Interactive Map -->
|
|
<div id="map" role="application" aria-label="Berlin Transit Live Map"></div>
|
|
|
|
<!-- Clock Display -->
|
|
<aside class="clock-container" role="complementary" aria-label="Aktuelle Uhrzeit und Datum">
|
|
<time class="time" id="time" aria-live="polite" aria-atomic="true"></time>
|
|
<div class="date" id="date" aria-live="polite" aria-atomic="true"></div>
|
|
</aside>
|
|
|
|
<!-- Loading Indicator -->
|
|
<div class="loading-indicator"
|
|
id="loadingIndicator"
|
|
role="status"
|
|
aria-live="polite"
|
|
aria-label="Ladezustand">
|
|
<div class="spinner" aria-hidden="true"></div>
|
|
<span class="loading-text">Lade Fahrzeuge...</span>
|
|
</div>
|
|
</main>
|
|
|
|
<script>
|
|
// ===== Application Initialization =====
|
|
'use strict';
|
|
|
|
/**
|
|
* Initialize the Leaflet map with custom settings
|
|
* Disables zoom controls for cleaner interface
|
|
*/
|
|
const map = L.map('map', {
|
|
zoomControl: false,
|
|
attributionControl: true,
|
|
zoomAnimation: true,
|
|
fadeAnimation: true,
|
|
markerZoomAnimation: true
|
|
});
|
|
|
|
// Add dark theme tile layer from CartoDB
|
|
L.tileLayer('https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}{r}.png', {
|
|
maxZoom: 19,
|
|
attribution: '© OpenStreetMap © CartoDB'
|
|
}).addTo(map);
|
|
|
|
// Set initial view to Berlin center
|
|
const BERLIN_CENTER = [52.52, 13.405];
|
|
const DEFAULT_ZOOM = 14;
|
|
map.setView(BERLIN_CENTER, DEFAULT_ZOOM);
|
|
|
|
// ===== Application State =====
|
|
const vehicleMarkers = {};
|
|
let userLocationMarker = null;
|
|
let isLoading = false;
|
|
let userPosition = null;
|
|
let updateInterval = null;
|
|
|
|
// ===== UI Helper Functions =====
|
|
|
|
/**
|
|
* Display the loading indicator
|
|
*/
|
|
function showLoading() {
|
|
const indicator = document.getElementById('loadingIndicator');
|
|
if (indicator) {
|
|
indicator.classList.add('show');
|
|
isLoading = true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Hide the loading indicator
|
|
*/
|
|
function hideLoading() {
|
|
const indicator = document.getElementById('loadingIndicator');
|
|
if (indicator) {
|
|
indicator.classList.remove('show');
|
|
isLoading = false;
|
|
}
|
|
}
|
|
|
|
// ===== Transit Product Type Helpers =====
|
|
|
|
/**
|
|
* Determine the vehicle type from product string
|
|
* @param {string} product - Product type from API
|
|
* @returns {string} Normalized product type
|
|
*/
|
|
function getProductType(product) {
|
|
if (!product) return 'bus';
|
|
const p = product.toLowerCase();
|
|
if (p.includes('subway') || p === 'subway') return 'subway';
|
|
if (p.includes('suburban') || p === 'suburban') return 'suburban';
|
|
if (p.includes('tram') || p === 'tram') return 'tram';
|
|
if (p.includes('bus') || p === 'bus') return 'bus';
|
|
if (p.includes('ferry')) return 'ferry';
|
|
if (p.includes('regional') || p.includes('express')) return 'regional';
|
|
return 'bus';
|
|
}
|
|
|
|
/**
|
|
* Get color associated with vehicle type
|
|
* @param {string} type - Vehicle type
|
|
* @returns {string} Hex color code
|
|
*/
|
|
function getProductColor(type) {
|
|
const colors = {
|
|
subway: '#0066cc',
|
|
suburban: '#00cc44',
|
|
tram: '#ff3333',
|
|
bus: '#cc44cc',
|
|
ferry: '#00ccff',
|
|
regional: '#ff9933'
|
|
};
|
|
return colors[type] || '#cc44cc';
|
|
}
|
|
|
|
/**
|
|
* Get icon letter for vehicle type
|
|
* @param {string} type - Vehicle type
|
|
* @returns {string} Single letter icon
|
|
*/
|
|
function getProductIcon(type) {
|
|
const icons = {
|
|
subway: 'U',
|
|
suburban: 'S',
|
|
tram: 'T',
|
|
bus: 'B',
|
|
ferry: 'F',
|
|
regional: 'R'
|
|
};
|
|
return icons[type] || 'B';
|
|
}
|
|
|
|
/**
|
|
* Get German name for vehicle type
|
|
* @param {string} type - Vehicle type
|
|
* @returns {string} Localized name
|
|
*/
|
|
function getProductName(type) {
|
|
const names = {
|
|
subway: 'U-Bahn',
|
|
suburban: 'S-Bahn',
|
|
tram: 'Straßenbahn',
|
|
bus: 'Bus',
|
|
ferry: 'Fähre',
|
|
regional: 'Regionalverkehr'
|
|
};
|
|
return names[type] || 'Bus';
|
|
}
|
|
|
|
// ===== Marker Creation Functions =====
|
|
|
|
/**
|
|
* Create custom icon for vehicle markers
|
|
* @param {string} type - Vehicle type
|
|
* @param {string} lineName - Line name to display
|
|
* @returns {L.DivIcon} Leaflet div icon
|
|
*/
|
|
function createVehicleIcon(type, lineName) {
|
|
const icon = getProductIcon(type);
|
|
return L.divIcon({
|
|
className: 'custom-vehicle-icon',
|
|
html: `
|
|
<div class="vehicle-marker-container">
|
|
<div class="vehicle-label">${lineName}</div>
|
|
<div class="vehicle-marker vehicle-${type}">${icon}</div>
|
|
</div>
|
|
`,
|
|
iconSize: [32, 56],
|
|
iconAnchor: [16, 28]
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Create custom icon for user location marker
|
|
* @returns {L.DivIcon} Leaflet div icon with pulse effect
|
|
*/
|
|
function createUserLocationIcon() {
|
|
return L.divIcon({
|
|
className: 'user-location-icon',
|
|
html: `
|
|
<div class="user-location-marker">
|
|
<div class="user-location-pulse"></div>
|
|
<div class="user-location-dot"></div>
|
|
</div>
|
|
`,
|
|
iconSize: [40, 40],
|
|
iconAnchor: [20, 20]
|
|
});
|
|
}
|
|
|
|
// ===== Data Loading Functions =====
|
|
|
|
/**
|
|
* Load vehicle data from VBB Radar API
|
|
* Fetches vehicles within current map bounds
|
|
* Updates existing markers or creates new ones
|
|
*/
|
|
async function loadRadarData() {
|
|
if (isLoading) return;
|
|
|
|
showLoading();
|
|
|
|
try {
|
|
// Get current map bounds for API request
|
|
const bounds = map.getBounds();
|
|
const north = bounds.getNorth().toFixed(6);
|
|
const south = bounds.getSouth().toFixed(6);
|
|
const west = bounds.getWest().toFixed(6);
|
|
const east = bounds.getEast().toFixed(6);
|
|
|
|
// Build API URL with bounds
|
|
const radarUrl = `https://v6.vbb.transport.rest/radar?north=${north}&south=${south}&west=${west}&east=${east}&results=150`;
|
|
|
|
// Fetch vehicle data with timeout
|
|
const controller = new AbortController();
|
|
const timeoutId = setTimeout(() => controller.abort(), 10000); // 10 second timeout
|
|
|
|
const response = await fetch(radarUrl, {
|
|
signal: controller.signal
|
|
});
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP Error ${response.status}: ${response.statusText}`);
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
// Validate response data
|
|
if (!data || typeof data !== 'object') {
|
|
throw new Error('Invalid response data format');
|
|
}
|
|
|
|
// Track current vehicle IDs to remove stale markers
|
|
const currentVehicleIds = new Set();
|
|
|
|
// Process vehicle movements from API
|
|
if (data.movements && Array.isArray(data.movements)) {
|
|
data.movements.forEach(movement => {
|
|
// Validate movement data
|
|
if (!movement || !movement.location || !movement.line) {
|
|
return; // Skip invalid movement
|
|
}
|
|
|
|
if (movement.location && movement.line) {
|
|
const type = getProductType(movement.line.product);
|
|
const lineName = movement.line.name || '?';
|
|
const icon = getProductIcon(type);
|
|
|
|
// Create unique vehicle ID
|
|
const vehicleId = movement.tripId || `${lineName}-${movement.direction || 'unknown'}`;
|
|
currentVehicleIds.add(vehicleId);
|
|
|
|
// Create new position
|
|
const newLatLng = L.latLng(movement.location.latitude, movement.location.longitude);
|
|
|
|
// Update existing marker or create new one
|
|
if (vehicleMarkers[vehicleId]) {
|
|
// Vehicle exists - update position with animation
|
|
const marker = vehicleMarkers[vehicleId].marker;
|
|
const oldLatLng = marker.getLatLng();
|
|
|
|
// Only animate if position changed
|
|
if (oldLatLng.lat !== newLatLng.lat || oldLatLng.lng !== newLatLng.lng) {
|
|
animateMarker(marker, oldLatLng, newLatLng, 3000);
|
|
}
|
|
} else {
|
|
// Create new marker for vehicle
|
|
const marker = L.marker(newLatLng, {
|
|
icon: createVehicleIcon(type, lineName),
|
|
riseOnHover: true
|
|
}).addTo(map);
|
|
|
|
const direction = movement.direction || 'Unbekannt';
|
|
const productName = getProductName(type);
|
|
|
|
// Build next stop information if available
|
|
let nextStopInfo = '';
|
|
if (movement.nextStopovers && Array.isArray(movement.nextStopovers) && movement.nextStopovers.length > 0) {
|
|
const nextStop = movement.nextStopovers[0];
|
|
if (nextStop.stop) {
|
|
const arrivalTime = nextStop.arrival ?
|
|
new Date(nextStop.arrival).toLocaleTimeString('de-DE', {
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
seconds: '2-digit'
|
|
}) : '—';
|
|
nextStopInfo = `
|
|
<div style="margin-top: 12px; padding-top: 12px; border-top: 1px solid rgba(255,255,255,0.2);">
|
|
<div style="font-size: 11px; color: rgba(255,255,255,0.6); margin-bottom: 4px;">
|
|
NÄCHSTER HALT
|
|
</div>
|
|
<div style="font-size: 13px; font-weight: 500;">
|
|
${nextStop.stop.name}
|
|
</div>
|
|
<div style="font-size: 12px; color: rgba(255,255,255,0.7); margin-top: 2px;">
|
|
Ankunft: ${arrivalTime}
|
|
</div>
|
|
</div>
|
|
`;
|
|
}
|
|
}
|
|
|
|
const popupContent = `
|
|
<div style="text-align: center; min-width: 200px;">
|
|
<div style="
|
|
width: 60px;
|
|
height: 60px;
|
|
margin: 0 auto 15px;
|
|
background: ${getProductColor(type)};
|
|
border-radius: 12px;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
font-size: 32px;
|
|
font-weight: 700;
|
|
color: white;
|
|
box-shadow: 0 4px 15px rgba(0,0,0,0.4);
|
|
">${icon}</div>
|
|
<div style="font-size: 11px; color: rgba(255,255,255,0.6); text-transform: uppercase; letter-spacing: 1px; margin-bottom: 6px;">
|
|
${productName}
|
|
</div>
|
|
<div style="font-weight: 700; font-size: 22px; margin-bottom: 8px; color: ${getProductColor(type)};">
|
|
${lineName}
|
|
</div>
|
|
<div style="font-size: 14px; color: rgba(255,255,255,0.85); line-height: 1.4;">
|
|
→ ${direction}
|
|
</div>
|
|
${nextStopInfo}
|
|
</div>
|
|
`;
|
|
marker.bindPopup(popupContent, {
|
|
maxWidth: 300,
|
|
className: 'custom-popup'
|
|
});
|
|
|
|
vehicleMarkers[vehicleId] = {
|
|
marker: marker,
|
|
type: type
|
|
};
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
// Remove vehicles that are no longer in the response
|
|
Object.keys(vehicleMarkers).forEach(vehicleId => {
|
|
if (!currentVehicleIds.has(vehicleId)) {
|
|
map.removeLayer(vehicleMarkers[vehicleId].marker);
|
|
delete vehicleMarkers[vehicleId];
|
|
}
|
|
});
|
|
|
|
console.log(`${Object.keys(vehicleMarkers).length} Fahrzeuge aktiv`);
|
|
|
|
} catch (error) {
|
|
// Handle different error types
|
|
if (error.name === 'AbortError') {
|
|
console.warn('Request timeout - Fahrzeugdaten konnten nicht geladen werden');
|
|
} else if (error instanceof TypeError && error.message.includes('fetch')) {
|
|
console.error('Netzwerkfehler - Bitte Internetverbindung prüfen:', error);
|
|
} else {
|
|
console.error('Fehler beim Laden der Fahrzeugdaten:', error);
|
|
}
|
|
|
|
// Show user-friendly error in UI (optional)
|
|
// You could add an error notification here
|
|
} finally {
|
|
hideLoading();
|
|
}
|
|
}
|
|
|
|
// ===== Animation Functions =====
|
|
|
|
/**
|
|
* Smoothly animate marker from one position to another
|
|
* @param {L.Marker} marker - Leaflet marker to animate
|
|
* @param {L.LatLng} startLatLng - Starting position
|
|
* @param {L.LatLng} endLatLng - Ending position
|
|
* @param {number} duration - Animation duration in milliseconds
|
|
*/
|
|
function animateMarker(marker, startLatLng, endLatLng, duration) {
|
|
const startTime = Date.now();
|
|
const latDiff = endLatLng.lat - startLatLng.lat;
|
|
const lngDiff = endLatLng.lng - startLatLng.lng;
|
|
|
|
function updatePosition() {
|
|
const elapsed = Date.now() - startTime;
|
|
const progress = Math.min(elapsed / duration, 1);
|
|
|
|
// Ease in-out cubic interpolation for smooth movement
|
|
const easeProgress = progress < 0.5
|
|
? 2 * progress * progress
|
|
: 1 - Math.pow(-2 * progress + 2, 2) / 2;
|
|
|
|
const currentLat = startLatLng.lat + (latDiff * easeProgress);
|
|
const currentLng = startLatLng.lng + (lngDiff * easeProgress);
|
|
|
|
marker.setLatLng([currentLat, currentLng]);
|
|
|
|
// Continue animation until complete
|
|
if (progress < 1) {
|
|
requestAnimationFrame(updatePosition);
|
|
}
|
|
}
|
|
|
|
updatePosition();
|
|
}
|
|
|
|
// ===== Geolocation Functions =====
|
|
|
|
/**
|
|
* Get user's current location and center map on it
|
|
* Falls back to Berlin center if geolocation is unavailable
|
|
*/
|
|
function getUserLocation() {
|
|
if (navigator.geolocation) {
|
|
navigator.geolocation.getCurrentPosition(
|
|
// Success callback
|
|
(position) => {
|
|
const lat = position.coords.latitude;
|
|
const lon = position.coords.longitude;
|
|
userPosition = [lat, lon];
|
|
|
|
// Center map on user location
|
|
map.setView([lat, lon], 16, {
|
|
animate: true,
|
|
duration: 1
|
|
});
|
|
|
|
// Remove old location marker if exists
|
|
if (userLocationMarker) {
|
|
map.removeLayer(userLocationMarker);
|
|
}
|
|
|
|
// Create new location marker
|
|
userLocationMarker = L.marker([lat, lon], {
|
|
icon: createUserLocationIcon(),
|
|
zIndexOffset: 1000
|
|
}).addTo(map);
|
|
|
|
// Add popup with coordinates
|
|
userLocationMarker.bindPopup(`
|
|
<div style="text-align: center;">
|
|
<div style="font-size: 32px; margin-bottom: 10px;">📍</div>
|
|
<div style="font-weight: 700; font-size: 16px; margin-bottom: 4px;">
|
|
Dein Standort
|
|
</div>
|
|
<div style="font-size: 12px; color: rgba(255,255,255,0.7);">
|
|
${lat.toFixed(4)}°, ${lon.toFixed(4)}°
|
|
</div>
|
|
</div>
|
|
`);
|
|
|
|
console.log('Standort gefunden:', lat, lon);
|
|
loadRadarData();
|
|
},
|
|
// Error callback
|
|
(error) => {
|
|
console.warn('Standort nicht verfügbar:', error.message);
|
|
// Load data with default Berlin view
|
|
loadRadarData();
|
|
},
|
|
// Options
|
|
{
|
|
enableHighAccuracy: true,
|
|
timeout: 5000,
|
|
maximumAge: 0
|
|
}
|
|
);
|
|
} else {
|
|
console.warn('Geolocation wird von diesem Browser nicht unterstützt');
|
|
loadRadarData();
|
|
}
|
|
}
|
|
|
|
// ===== Clock Functions =====
|
|
|
|
/**
|
|
* Update clock display with current time and date
|
|
* Formats time as HH:MM:SS and date in German format
|
|
*/
|
|
function updateClock() {
|
|
const now = new Date();
|
|
|
|
// Format time as HH:MM:SS
|
|
const hours = String(now.getHours()).padStart(2, '0');
|
|
const minutes = String(now.getMinutes()).padStart(2, '0');
|
|
const seconds = String(now.getSeconds()).padStart(2, '0');
|
|
|
|
const timeElement = document.getElementById('time');
|
|
if (timeElement) {
|
|
timeElement.textContent = `${hours}:${minutes}:${seconds}`;
|
|
}
|
|
|
|
// Format date in German locale
|
|
const options = {
|
|
weekday: 'long',
|
|
year: 'numeric',
|
|
month: 'long',
|
|
day: 'numeric'
|
|
};
|
|
const dateStr = now.toLocaleDateString('de-DE', options);
|
|
const dateElement = document.getElementById('date');
|
|
if (dateElement) {
|
|
dateElement.textContent = dateStr;
|
|
}
|
|
}
|
|
|
|
// ===== Event Listeners =====
|
|
|
|
/**
|
|
* Debounced map movement handler
|
|
* Reloads vehicle data when map stops moving
|
|
*/
|
|
let moveEndTimeout;
|
|
map.on('moveend', () => {
|
|
clearTimeout(moveEndTimeout);
|
|
moveEndTimeout = setTimeout(() => {
|
|
loadRadarData();
|
|
}, 300);
|
|
});
|
|
|
|
// ===== Application Startup =====
|
|
|
|
// Initialize clock display
|
|
updateClock();
|
|
|
|
// Get user location and load initial data
|
|
getUserLocation();
|
|
|
|
// Update clock every second
|
|
setInterval(updateClock, 1000);
|
|
|
|
// Refresh vehicle data every 15 seconds
|
|
setInterval(() => {
|
|
if (!isLoading) {
|
|
loadRadarData();
|
|
}
|
|
}, 15000);
|
|
|
|
// Log application start
|
|
console.log('Berlin Live Transit gestartet');
|
|
</script>
|
|
</body>
|
|
</html> |