I am new to SwiftUI and I'm trying to pass in some coordinates from an object I have inside a NavigationStack
I feel my approach is correct but the error suggests it needs a two way binding property which I don't have in this instance.
I tried setting a variable called var region and then passing that to the Map but I don't think I can use code in the middle of a View ?
What am I doing wrong?
struct ContentView: View {
var chargingSessions: [ChargingSession] = [ChargingSession(id: 1, Name: "John", Kwh: 2.003, Price: 3.04, StartDate: Date.now.addingTimeInterval(-10000), Latitude: 59.9139, Longitude: 10.7522),
ChargingSession(id: 2, Name: "Dave", Kwh: 4.003, Price: 6.04, StartDate: Date.now.addingTimeInterval(-1000000),Latitude: 59.9239, Longitude: 10.7622)]
@State private var mapRegion = MKCoordinateRegion()
let regionSize = 500.0
var body: some View {
TabView {
Text("Home Screen")
.tabItem {
Label("Home",systemImage: "house.fill")
}
NavigationStack {
List(chargingSessions) { chargingSession in
HStack{
Image(systemName: "ev.charger.fill")
.foregroundColor(Color(red: 0.031, green: 0.251, blue: 0.302, opacity: 1.0))
.imageScale(.large)
.padding(.trailing, 5.0)
VStack{
NavigationLink(chargingSession.StartDate.formatted(.relative(presentation:.named)), value:chargingSession).fontWeight(.bold)
Text(chargingSession.Price.formatted()+"kr")
.foregroundColor(.gray)
.frame(maxWidth: .infinity, alignment: .leading)
Text(chargingSession.Kwh.formatted()+"kWh")
.foregroundColor(.gray)
.frame(maxWidth: .infinity, alignment: .leading)
}
}
}
.navigationTitle("Charging Sessions")
.navigationDestination(for: ChargingSession.self) { chargingSession in
Map(coordinateRegion: MKCoordinateRegion(center: chargingSession.Coordinate, latitudinalMeters: regionSize, longitudinalMeters: regionSize))
}
}
.tabItem {
Image(systemName: "bolt.car.fill")
Text("Sessions")
}
Text("Profile Screen")
.tabItem {
Label("Profile",systemImage: "person.crop.circle.fill")
}
}
.tint(Color(red: 0.031, green: 0.251, blue: 0.302))
}
}