-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: constructed rough RoutePoint, Route and RoutMapView structs
added basic route stuct that holds coordinates represented as routePoints as well as built helper functions for bounding box calculations
- Loading branch information
Showing
5 changed files
with
143 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// | ||
// Route.swift | ||
// windrider-ios | ||
// | ||
// Created by Daniel Grbac Bravo on 01/03/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
struct Route: Codable { | ||
let routeId: String? | ||
let points: [RoutePoint] | ||
|
||
init(points: [RoutePoint]) { | ||
self.routeId = nil | ||
self.points = points | ||
} | ||
|
||
// finds the bounding box of the route | ||
// returns the two points that define the bounding box | ||
public func caluateBoundingBox(route: Route) -> [RoutePoint]{ | ||
var minLat: Double = 90 | ||
var maxLat: Double = -90 | ||
var minLon: Double = 180 | ||
var maxLon: Double = -180 | ||
for point in route.points { | ||
if point.latitude < minLat { | ||
minLat = point.latitude | ||
} | ||
if point.latitude > maxLat { | ||
maxLat = point.latitude | ||
} | ||
if point.longitude < minLon { | ||
minLon = point.longitude | ||
} | ||
if point.longitude > maxLon { | ||
maxLon = point.longitude | ||
} | ||
} | ||
return [RoutePoint(latitude: minLat, longitude: minLon, direction: 0, timestamp: Date()), RoutePoint(latitude: maxLat, longitude: maxLon, direction: 0, timestamp: Date())] | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
|
||
struct RoutePoint: Codable { | ||
let latitude: Double | ||
let longitude: Double | ||
let direction: Double | ||
let timestamp: Date | ||
|
||
init(latitude: Double, longitude: Double, direction: Double, timestamp: Date) { | ||
self.latitude = latitude | ||
self.longitude = longitude | ||
self.direction = direction | ||
self.timestamp = timestamp | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// | ||
// RouteMapView.swift | ||
// windrider-ios | ||
// | ||
// Created by Daniel Grbac Bravo on 01/03/2024. | ||
// | ||
|
||
import SwiftUI | ||
import MapKit | ||
|
||
func calculateRegion(boundingBox: [RoutePoint]) -> MKCoordinateRegion { | ||
let minLat = boundingBox[0].latitude | ||
let maxLat = boundingBox[1].latitude | ||
let minLon = boundingBox[0].longitude | ||
let maxLon = boundingBox[1].longitude | ||
|
||
let center = CLLocationCoordinate2D(latitude: (minLat + maxLat) / 2, longitude: (minLon + maxLon) / 2) | ||
let span = MKCoordinateSpan(latitudeDelta: (maxLat - minLat) * 1.1, longitudeDelta: (maxLon - minLon) * 1.1) | ||
|
||
return MKCoordinateRegion(center: center, span: span) | ||
} | ||
|
||
|
||
|
||
|
||
struct RouteMapView: View { | ||
var route: Route | ||
@State private var region: MKCoordinateRegion | ||
|
||
init(route: Route) { | ||
self.route = route | ||
let boundingBox = route.caluateBoundingBox(route: route) // Assuming this is a correct method call | ||
self._region = State(initialValue: calculateRegion(boundingBox: boundingBox)) | ||
} | ||
|
||
var body: some View { | ||
ZStack{ | ||
Map(){ | ||
|
||
} | ||
.ignoresSafeArea() | ||
|
||
} | ||
} | ||
} | ||
|
||
|
||
struct RouteMapView_Previews: PreviewProvider { | ||
static func createRandomRoute() -> Route { | ||
var points = [RoutePoint]() | ||
for _ in 0...100 { | ||
points.append(RoutePoint(latitude: Double.random(in: 53.0...54.0), longitude: Double.random(in: 6.0...7.0), direction: Double.random(in: 0...360), timestamp: Date())) | ||
} | ||
return Route(points: points) | ||
} | ||
|
||
static var previews: some View { | ||
// Now this call is valid because createRandomRoute is a static method | ||
let randomRoute = createRandomRoute() | ||
|
||
RouteMapView(route: randomRoute) | ||
} | ||
} |