admin管理员组文章数量:1122832
import SwiftUI
import MapKit
import CoreLocation
struct ContentView: View {
@State private var position: MapCameraPosition = .userLocation(fallback: .automatic)
@State private var hazards: [(Double, Double)] = [] // Array of tuples for hazards
@StateObject private var locationManager = LocationManager() // Reference to your LocationManager
var body: some View {
ZStack {
// Map with annotations
error here >Map(position: $position) {
UserAnnotation()
// Iterate over hazards to mark them
for hazard in hazards {
MapAnnotation(coordinate: CLLocationCoordinate2D(latitude: hazard.0, longitude: hazard.1)) {
VStack {
Image(systemName: "exclamationmark.triangle.fill")
.foregroundColor(.red)
.font(.title)
Text("Hazard")
.font(.caption)
.padding(4)
.background(Color.white.opacity(0.8))
.cornerRadius(5)
}
}
}
}
}
}
}
I am trying to iterate through an array that contains the coordinates of the user location when the button is pressed. Right now I only need to process each set of coordinates and mark a spot on the map at those coordinates. I am accurately getting the coordinates but the line,
Map(position: $position) {
gives an error:
Trailing closure passed to parameter of type 'Binding<MapFeature?>' that does not accept a closure"
If I comment out:
for hazard in hazards {
Then it goes away and you get a bunch of errors that just conform to the line being commented out. The rest of the code has logic for a button that is pressed and adds coordinates to the hazards array of tuples. ie [(42.7963, -71.1545)] and other MapKit UI code that is irrelevant to the question.
I have tried changing the $position to an MKcoordinate that chatgpt suggested but it just created more problems and didn't help. This is my first time programming in swift so perhaps there is an easy fix but I cannot figure it out.
import SwiftUI
import MapKit
import CoreLocation
struct ContentView: View {
@State private var position: MapCameraPosition = .userLocation(fallback: .automatic)
@State private var hazards: [(Double, Double)] = [] // Array of tuples for hazards
@StateObject private var locationManager = LocationManager() // Reference to your LocationManager
var body: some View {
ZStack {
// Map with annotations
error here >Map(position: $position) {
UserAnnotation()
// Iterate over hazards to mark them
for hazard in hazards {
MapAnnotation(coordinate: CLLocationCoordinate2D(latitude: hazard.0, longitude: hazard.1)) {
VStack {
Image(systemName: "exclamationmark.triangle.fill")
.foregroundColor(.red)
.font(.title)
Text("Hazard")
.font(.caption)
.padding(4)
.background(Color.white.opacity(0.8))
.cornerRadius(5)
}
}
}
}
}
}
}
I am trying to iterate through an array that contains the coordinates of the user location when the button is pressed. Right now I only need to process each set of coordinates and mark a spot on the map at those coordinates. I am accurately getting the coordinates but the line,
Map(position: $position) {
gives an error:
Trailing closure passed to parameter of type 'Binding<MapFeature?>' that does not accept a closure"
If I comment out:
for hazard in hazards {
Then it goes away and you get a bunch of errors that just conform to the line being commented out. The rest of the code has logic for a button that is pressed and adds coordinates to the hazards array of tuples. ie [(42.7963, -71.1545)] and other MapKit UI code that is irrelevant to the question.
I have tried changing the $position to an MKcoordinate that chatgpt suggested but it just created more problems and didn't help. This is my first time programming in swift so perhaps there is an easy fix but I cannot figure it out.
Share Improve this question asked yesterday JacklanJacklan 113 bronze badges New contributor Jacklan is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.1 Answer
Reset to default 0Inside Map
you need to have Views
, not procedural code. So try using this approach using a dedicated struct Hazard
instead of an
array of tuples, and importantly a ForEach
loop instead of your procedural for hazard in hazards
loop, to display the hazards
on the Map.
Note MapAnnotation
is deprecated use Annotation
instead.
Example code:
struct Hazard: Identifiable {
let id: UUID = UUID()
let latitude: Double
let longitude: Double
}
struct ContentView: View {
@State private var position: MapCameraPosition = .userLocation(fallback: .automatic)
@State private var hazards: [Hazard] = [
Hazard(latitude: 35.69, longitude: 139.76),
Hazard(latitude: 35.70, longitude: 139.77)
] // Array of hazards for testing
// @StateObject private var locationManager = LocationManager() // Reference to your LocationManager
var body: some View {
Map(position: $position) {
UserAnnotation()
// Iterate over hazards to mark them
ForEach(hazards) { hazard in
Annotation("", coordinate: CLLocationCoordinate2D(latitude: hazard.latitude, longitude: hazard.longitude)) {
VStack {
Image(systemName: "exclamationmark.triangle.fill")
.foregroundColor(.red)
.font(.title)
Text("Hazard")
.font(.caption)
.padding(4)
.background(Color.white.opacity(0.8))
.cornerRadius(5)
}
}
}
}
}
}
本文标签:
版权声明:本文标题:swiftui - ERROR : " Trailing closure passed to parameter of type 'Binding<MapFeature?>' t 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736283868a1927109.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论