feat(wind): Live-Windrichtung vom Standort (Open-Meteo) (v1.6.1)
- Neuer „Live vom Standort"-Knopf in der Windrose holt die aktuelle Windrichtung + Geschwindigkeit von Open-Meteo (kostenlos, ohne Key) - Beim Öffnen eines Einsatzes ohne gesetzten Wind wird die Windrichtung automatisch vom Einsatzort vorgeschlagen (einmal pro Projekt, online) - Toast zeigt Richtung, Grad, km/h und Böen - Neue lib/wind.ts (fetchLiveWind) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -27,6 +27,8 @@ import { useMapExport } from '@/hooks/use-map-export'
|
||||
import { useAutoSave } from '@/hooks/use-auto-save'
|
||||
import { useOfflineSync } from '@/hooks/use-offline-sync'
|
||||
import { preloadTilesForArea, shouldPreloadForProject, markPreloadedForProject } from '@/lib/offline-tiles'
|
||||
import { fetchLiveWind } from '@/lib/wind'
|
||||
import { degToCompass } from '@/components/map/map-compass'
|
||||
import { useRealtimeSync } from '@/hooks/use-realtime-sync'
|
||||
import type { Project, ProjectMode, DrawFeature, Feature, JournalEntry, DrawMode } from '@/types'
|
||||
import { useToolStore } from '@/stores/tool-store'
|
||||
@@ -484,6 +486,39 @@ export default function AppPage() {
|
||||
} catch { /* Komfort — Fehler still ignorieren */ }
|
||||
}, [currentProject])
|
||||
|
||||
// Live-Wind von Open-Meteo für den aktuellen Kartenausschnitt (Einsatzort) holen
|
||||
const handleFetchLiveWind = useCallback(async () => {
|
||||
if (!currentProject) return
|
||||
const c = mapRef.current?.getCenter?.() || currentProject.mapCenter
|
||||
if (!c) return
|
||||
const wind = await fetchLiveWind(c.lat, c.lng)
|
||||
if (!wind) {
|
||||
toast({ title: 'Wind nicht verfügbar', description: 'Keine Live-Daten für diesen Standort.', variant: 'destructive' })
|
||||
return
|
||||
}
|
||||
await handleWindChange(wind.direction)
|
||||
toast({
|
||||
title: 'Live-Wind übernommen',
|
||||
description: `aus ${degToCompass(wind.direction)} (${wind.direction}°) · ${Math.round(wind.speed)} km/h${wind.gusts ? `, Böen ${Math.round(wind.gusts)} km/h` : ''}`,
|
||||
})
|
||||
}, [currentProject, handleWindChange, toast])
|
||||
|
||||
// Beim Öffnen eines Einsatzes ohne gesetzten Wind automatisch die aktuelle
|
||||
// Windrichtung vorschlagen (nur einmal pro Projekt, online).
|
||||
const autoWindRef = useRef<string | null>(null)
|
||||
useEffect(() => {
|
||||
const proj = currentProject
|
||||
if (!proj?.id || typeof navigator === 'undefined' || !navigator.onLine) return
|
||||
if (typeof proj.windDirection === 'number') return
|
||||
if (autoWindRef.current === proj.id) return
|
||||
autoWindRef.current = proj.id
|
||||
const c = proj.mapCenter
|
||||
if (!c?.lat || !c?.lng) return
|
||||
fetchLiveWind(c.lat, c.lng).then((wind) => {
|
||||
if (wind) handleWindChange(wind.direction)
|
||||
})
|
||||
}, [currentProject?.id, currentProject?.windDirection, handleWindChange])
|
||||
|
||||
// Fullscreen toggle
|
||||
const toggleFullscreen = useCallback(() => {
|
||||
if (!document.fullscreenElement) {
|
||||
@@ -1063,6 +1098,7 @@ export default function AppPage() {
|
||||
onToggleFullscreen={toggleFullscreen}
|
||||
windDirection={currentProject?.windDirection}
|
||||
onWindChange={handleWindChange}
|
||||
onFetchLiveWind={handleFetchLiveWind}
|
||||
/>
|
||||
</main>
|
||||
</div>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import { Wind, X } from 'lucide-react'
|
||||
import { Wind, X, Loader2, Satellite } from 'lucide-react'
|
||||
|
||||
interface MapCompassProps {
|
||||
/** Karten-Bearing in Grad (0 = Norden oben) */
|
||||
@@ -10,6 +10,8 @@ interface MapCompassProps {
|
||||
windDirection: number | null | undefined
|
||||
canEdit: boolean
|
||||
onWindChange: (dir: number | null) => void
|
||||
/** Live-Wind (Open-Meteo) für den aktuellen Standort holen */
|
||||
onFetchLiveWind?: () => Promise<void> | void
|
||||
}
|
||||
|
||||
const DIRS = ['N', 'NO', 'O', 'SO', 'S', 'SW', 'W', 'NW']
|
||||
@@ -24,10 +26,17 @@ const WIND_OPTIONS: { label: string; deg: number }[] = [
|
||||
{ label: 'S', deg: 180 }, { label: 'SW', deg: 225 }, { label: 'W', deg: 270 }, { label: 'NW', deg: 315 },
|
||||
]
|
||||
|
||||
export function MapCompass({ bearing, windDirection, canEdit, onWindChange }: MapCompassProps) {
|
||||
export function MapCompass({ bearing, windDirection, canEdit, onWindChange, onFetchLiveWind }: MapCompassProps) {
|
||||
const [open, setOpen] = useState(false)
|
||||
const [loadingLive, setLoadingLive] = useState(false)
|
||||
const hasWind = typeof windDirection === 'number'
|
||||
|
||||
const handleLive = async () => {
|
||||
if (!onFetchLiveWind) return
|
||||
setLoadingLive(true)
|
||||
try { await onFetchLiveWind() } finally { setLoadingLive(false) }
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="absolute top-3 left-[60px] md:left-3 z-10 flex flex-col items-center gap-1.5">
|
||||
{/* Kompass mit Nordpfeil (dreht mit dem Karten-Bearing) */}
|
||||
@@ -75,6 +84,18 @@ export function MapCompass({ bearing, windDirection, canEdit, onWindChange }: Ma
|
||||
<span className="text-xs font-semibold flex items-center gap-1"><Wind className="w-3.5 h-3.5" /> Wind aus</span>
|
||||
<button onClick={() => setOpen(false)} className="text-muted-foreground hover:text-foreground"><X className="w-3.5 h-3.5" /></button>
|
||||
</div>
|
||||
{onFetchLiveWind && (
|
||||
<button
|
||||
onClick={handleLive}
|
||||
disabled={loadingLive}
|
||||
className="w-full mb-1.5 flex items-center justify-center gap-1.5 rounded-md bg-blue-600 hover:bg-blue-700 text-white text-xs font-semibold py-1.5 disabled:opacity-60"
|
||||
title="Aktuelle Windrichtung vom Standort (Open-Meteo)"
|
||||
>
|
||||
{loadingLive ? <Loader2 className="w-3.5 h-3.5 animate-spin" /> : <Satellite className="w-3.5 h-3.5" />}
|
||||
Live vom Standort
|
||||
</button>
|
||||
)}
|
||||
<div className="text-[10px] text-muted-foreground mb-1 px-0.5">oder Richtung wählen:</div>
|
||||
<div className="grid grid-cols-4 gap-1">
|
||||
{WIND_OPTIONS.map((w) => (
|
||||
<button
|
||||
|
||||
@@ -95,6 +95,8 @@ interface MapViewProps {
|
||||
/** Windrichtung fürs Lagebild (Grad, „Wind aus") + Setter */
|
||||
windDirection?: number | null
|
||||
onWindChange?: (dir: number | null) => void
|
||||
/** Live-Wind (Open-Meteo) für den aktuellen Standort holen */
|
||||
onFetchLiveWind?: () => Promise<void> | void
|
||||
}
|
||||
|
||||
export function MapView({
|
||||
@@ -116,6 +118,7 @@ export function MapView({
|
||||
onToggleFullscreen,
|
||||
windDirection,
|
||||
onWindChange,
|
||||
onFetchLiveWind,
|
||||
}: MapViewProps) {
|
||||
const mapContainer = useRef<HTMLDivElement | null>(null)
|
||||
const map = useRef<maplibregl.Map | null>(null)
|
||||
@@ -2209,6 +2212,7 @@ export function MapView({
|
||||
windDirection={windDirection}
|
||||
canEdit={canEdit}
|
||||
onWindChange={(dir) => onWindChange?.(dir)}
|
||||
onFetchLiveWind={onFetchLiveWind}
|
||||
/>
|
||||
|
||||
{/* Moveable controls for selected symbol */}
|
||||
|
||||
32
src/lib/wind.ts
Normal file
32
src/lib/wind.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Live-Winddaten von Open-Meteo (kostenlos, ohne API-Key).
|
||||
* Liefert die aktuelle Windrichtung (meteorologisch „Wind aus", Grad) und
|
||||
* Geschwindigkeit (km/h) am gegebenen Standort.
|
||||
*/
|
||||
export interface LiveWind {
|
||||
direction: number // 0..359, meteorologisch „Wind aus"
|
||||
speed: number // km/h
|
||||
gusts?: number // km/h, Böen
|
||||
}
|
||||
|
||||
export async function fetchLiveWind(lat: number, lng: number): Promise<LiveWind | null> {
|
||||
try {
|
||||
const url =
|
||||
`https://api.open-meteo.com/v1/forecast?latitude=${lat.toFixed(4)}&longitude=${lng.toFixed(4)}` +
|
||||
`¤t=wind_speed_10m,wind_direction_10m,wind_gusts_10m`
|
||||
const res = await fetch(url)
|
||||
if (!res.ok) return null
|
||||
const data = await res.json()
|
||||
const dir = data?.current?.wind_direction_10m
|
||||
const spd = data?.current?.wind_speed_10m
|
||||
const gust = data?.current?.wind_gusts_10m
|
||||
if (typeof dir !== 'number') return null
|
||||
return {
|
||||
direction: ((Math.round(dir) % 360) + 360) % 360,
|
||||
speed: typeof spd === 'number' ? spd : 0,
|
||||
gusts: typeof gust === 'number' ? gust : undefined,
|
||||
}
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user