I have been exploring using maps in swiftUI and have added annotations from my data, however when I work with adding a navigation link into the annotations I receive an error - "Contextual closure type '() -> siteView' expects 0 arguments, but 1 was used in closure body" problem is that siteView() does expect a Site to be passed through.
Question is - is there a way to resolve this and pass the site used to create the annotation through to the siteView() screen?
Code below:
struct siteMapView: View {
@EnvironmentObject var siteData: siteData
@State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), span: MKCoordinateSpan(latitudeDelta: 10, longitudeDelta: 10))
var body: some View {
NavigationView {
Map(coordinateRegion: $region, annotationItems: siteData.sites) {
MapAnnotation(coordinate: $0.locationCoordinate, anchorPoint: CGPoint(x: 0.5, y: 0.5)) {
NavigationLink {
siteView(site: $0)
} label: {
Circle()
.strokeBorder(Color.green, lineWidth: 10)
.frame(width: 44, height: 44)
}
}
}
.edgesIgnoringSafeArea(.top)
}
}
}
siteView code for completeness:
struct siteView: View {
@EnvironmentObject var siteData: siteData
var site: Site
var body: some View {
ScrollView {
VStack {
ZStack{
AsyncImage(url: URL(string: "https://overseer.cyou/heritage/images/\(site.imageFile).jpg")) { phase in
switch phase {
case .empty:
ProgressView()
case .success(let image):
image
.resizable()
.clipped()
.aspectRatio(1, contentMode: .fill)
.frame(maxWidth: .infinity)
case .failure:
Text("Failure")
Image(systemName: "photo")
@unknown default:
EmptyView()
}
}
VStack {
Spacer()
VStack {
Text(site.name)
.font(.title)
.foregroundColor(Color.white)
Text(site.country)
}
.frame(maxWidth: .infinity)
.background(Color.black)
//.opacity(0.2)
}
}
Text(site.description)
}
}
}
}
}
CodePudding user response:
Map(coordinateRegion: $region, annotationItems: siteData.sites) { site in
MapAnnotation(coordinate: site.locationCoordinate, anchorPoint: CGPoint(x: 0.5, y: 0.5)) {
NavigationLink {
// '$0' ↑
// you can't use '$0' here
// siteView(site: $0)
siteView(site: site)
} label: {
Circle()
.strokeBorder(Color.green, lineWidth: 10)
.frame(width: 44, height: 44)
}
}
}
.edgesIgnoringSafeArea(.top)