From f379d2d9b9cc1e5613ce18f8024b51239a70abf2 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Thu, 21 Nov 2024 15:54:18 +0000 Subject: [PATCH] centre map on loaded track --- frontend/src/Main.elm | 9 ++++++++- frontend/src/Point.elm | 26 +++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/frontend/src/Main.elm b/frontend/src/Main.elm index 0e8eb93..901fd64 100644 --- a/frontend/src/Main.elm +++ b/frontend/src/Main.elm @@ -166,7 +166,7 @@ init _ url navKey = Just (Timeline (Just s) (Just d)) -> (s, d) _ -> (10,10) in - ((Model (toCoord 51.60 -0.01) (FineZoomLevel (13*8)) None 0 0 Empty), + ((Model (toCoord 0 0) (FineZoomLevel (1*8)) None 0 0 Empty), (fetchTrack start duration)) -- SUBSCRIPTIONS @@ -198,6 +198,11 @@ type Msg | UrlChanged | Dribble String + +coordFromPos p = + let {lat, lon } = p + in toCoord lat lon + update : Msg -> Model -> (Model, Cmd Msg) update msg model = (updateModel msg model, Cmd.none) @@ -243,6 +248,8 @@ updateModel msg model = in { model | track = Present trk + , centre = coordFromPos (Point.centre trk) + , zoom = FineZoomLevel ( 13 * 8) , startTime = floor start , duration = ceiling duration } diff --git a/frontend/src/Point.elm b/frontend/src/Point.elm index 6179249..dc84166 100644 --- a/frontend/src/Point.elm +++ b/frontend/src/Point.elm @@ -1,4 +1,4 @@ -module Point exposing(Pos, Point, decoder, downsample, duration, subseq, startTime) +module Point exposing(Pos, Point, decoder, downsample, duration, subseq, startTime, centre) import Json.Decode as D @@ -68,6 +68,30 @@ startTime points = (p::ps) -> Just p.time _ -> Nothing +type Bound = Bound Pos Pos | NoBound + +extendBound : Pos -> Bound -> Bound +extendBound pos b = + let {lat, lon} = pos + in case b of + (Bound p1 p2) -> + Bound + (Pos (min lat p1.lat) (min lon p1.lon) Nothing) + (Pos (max lat p2.lat) (max lon p2.lon) Nothing) + NoBound -> + Bound pos pos + +bounds points = + List.foldr extendBound NoBound (List.map .pos points) + +centre points = + case bounds points of + Bound min max -> Pos + ((max.lat + min.lat) / 2) + ((max.lon + min.lon) / 2) + Nothing + NoBound -> Pos 0 0 Nothing + subseq points start dur = case points of [] -> []