feat(route): display route on the map

This commit is contained in:
2024-11-25 14:49:55 +01:00
parent f8e5a0b506
commit 92e3a2aa9f
2 changed files with 32 additions and 16 deletions

View File

@@ -5,15 +5,16 @@
// Created by Oliver Hnát on 23.11.2024. // Created by Oliver Hnát on 23.11.2024.
// //
import SwiftUI
import MapKit import MapKit
import SwiftUI
struct ContentView: View { struct ContentView: View {
@ObservedObject var viewModel = ViewModel() @ObservedObject var viewModel = ViewModel()
@State private var position = MapCameraPosition.automatic @State private var position = MapCameraPosition.automatic
@State private var showSearch: Bool = true @State private var showSearch: Bool = true
@State private var directions: [MKRoute] = []
// TODO: create a map // TODO: create a map
// Add navigation to the map // Add navigation to the map
// Display the calculated distance and how long will it take by walking // Display the calculated distance and how long will it take by walking
@@ -23,21 +24,36 @@ struct ContentView: View {
// show how long does the route take with said walking speed // show how long does the route take with said walking speed
var body: some View { var body: some View {
Map(position: $position) Map(position: $position) {
// .ignoresSafeArea() ForEach(0..<directions.count) { i in
.sheet(isPresented: $showSearch, content: { MapPolyline(directions[i].polyline)
SearchView() .stroke(Constants.routeColor[i], lineWidth: Constants.routeWidth)
}
}
// .ignoresSafeArea()
.sheet(
isPresented: $showSearch,
content: {
SearchView(directions: $directions)
}) })
// Text("This is what's set: \(viewModel.test)") // Text("This is what's set: \(viewModel.test)")
// Button(action: { // Button(action: {
// save(value: "te5t3") // save(value: "te5t3")
// }, label: {Text("CLICK ME")}) // }, label: {Text("CLICK ME")})
} }
func save(value: String) { func save(value: String) {
viewModel.saveValue(value) viewModel.saveValue(value)
} }
} }
enum Constants {
static let routeColor: [Color] = [
.blue,
.red,
.yellow,
]
static let routeWidth: CGFloat = 8
}
#Preview { #Preview {
ContentView() ContentView()
} }

View File

@@ -11,6 +11,7 @@ import SwiftUI
struct SearchView: View { struct SearchView: View {
@State private var query: String = "" @State private var query: String = ""
@State private var locations: [MKMapItem] = [] @State private var locations: [MKMapItem] = []
@Binding var directions: [MKRoute]
var body: some View { var body: some View {
VStack { VStack {
@@ -60,9 +61,7 @@ struct SearchView: View {
print("Error while searching for directions") print("Error while searching for directions")
return return
} }
for route in response.routes { self.directions = response.routes
// extract route(s)
}
} }
} }
@@ -102,5 +101,6 @@ struct TextFieldGrayBackgroudColor: ViewModifier {
} }
#Preview { #Preview {
SearchView() @Previewable @State var directions: [MKRoute] = []
return SearchView(directions: $directions)
} }