feat(route): display route on the map
This commit is contained in:
@@ -5,15 +5,16 @@
|
||||
// Created by Oliver Hnát on 23.11.2024.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import MapKit
|
||||
import SwiftUI
|
||||
|
||||
struct ContentView: View {
|
||||
|
||||
@ObservedObject var viewModel = ViewModel()
|
||||
|
||||
@State private var position = MapCameraPosition.automatic
|
||||
@State private var showSearch: Bool = true
|
||||
@State private var directions: [MKRoute] = []
|
||||
|
||||
// TODO: create a map
|
||||
// Add navigation to the map
|
||||
// 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
|
||||
|
||||
var body: some View {
|
||||
Map(position: $position)
|
||||
// .ignoresSafeArea()
|
||||
.sheet(isPresented: $showSearch, content: {
|
||||
SearchView()
|
||||
Map(position: $position) {
|
||||
ForEach(0..<directions.count) { i in
|
||||
MapPolyline(directions[i].polyline)
|
||||
.stroke(Constants.routeColor[i], lineWidth: Constants.routeWidth)
|
||||
}
|
||||
}
|
||||
// .ignoresSafeArea()
|
||||
.sheet(
|
||||
isPresented: $showSearch,
|
||||
content: {
|
||||
SearchView(directions: $directions)
|
||||
})
|
||||
// Text("This is what's set: \(viewModel.test)")
|
||||
// Button(action: {
|
||||
// save(value: "te5t3")
|
||||
// }, label: {Text("CLICK ME")})
|
||||
// Text("This is what's set: \(viewModel.test)")
|
||||
// Button(action: {
|
||||
// save(value: "te5t3")
|
||||
// }, label: {Text("CLICK ME")})
|
||||
}
|
||||
|
||||
func save(value: String) {
|
||||
viewModel.saveValue(value)
|
||||
}
|
||||
}
|
||||
enum Constants {
|
||||
static let routeColor: [Color] = [
|
||||
.blue,
|
||||
.red,
|
||||
.yellow,
|
||||
]
|
||||
static let routeWidth: CGFloat = 8
|
||||
}
|
||||
#Preview {
|
||||
ContentView()
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import SwiftUI
|
||||
struct SearchView: View {
|
||||
@State private var query: String = ""
|
||||
@State private var locations: [MKMapItem] = []
|
||||
@Binding var directions: [MKRoute]
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
@@ -60,9 +61,7 @@ struct SearchView: View {
|
||||
print("Error while searching for directions")
|
||||
return
|
||||
}
|
||||
for route in response.routes {
|
||||
// extract route(s)
|
||||
}
|
||||
self.directions = response.routes
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,5 +101,6 @@ struct TextFieldGrayBackgroudColor: ViewModifier {
|
||||
}
|
||||
|
||||
#Preview {
|
||||
SearchView()
|
||||
@Previewable @State var directions: [MKRoute] = []
|
||||
return SearchView(directions: $directions)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user