我正在使用 SwiftUI 显示地图,如果用户点击注释,它会在 VStack 中弹出一个详细视图。我已经制作了地图视图并在另一个 SwiftUI 文件中插入了注释。我还做了详细视图。
如何在主视图文件中访问该地图的注释以定义 .tapaction 以便他们将其用于详细视图?
我尝试将视图定义为 MKMapView,但无法为另一个 SwiftUI 视图中的 UIViewRepresentable 执行此操作。
主视图(ContentView)代码为:
struct ContentView: View {
@State private var chosen = false
var body: some View {
VStack {
MapView()
.edgesIgnoringSafeArea(.top)
.frame(height: chosen ? 600:nil)
.tapAction {
withAnimation{ self.chosen.toggle()}
}
if chosen {
ExtractedView()
}
}
}
}
地图视图代码是:
struct MapView : UIViewRepresentable {
@State private var userLocationIsEnabled = false
var locationManager = CLLocationManager()
func makeUIView(context: Context) -> MKMapView {
MKMapView(frame: .zero)
}
func updateUIView(_ view: MKMapView, context: Context) {
view.showsUserLocation = true
.
.
.
let sampleCoordinates = [
CLLocation(latitude: xx.xxx, longitude: xx.xxx),
CLLocation(latitude: xx.xxx, longitude: xx.xxx),
CLLocation(latitude: xx.xxx, longitude: xx.xxx)
]
addAnnotations(coords: sampleCoordinates, view: view)
}
}
}
我希望能够访问地图视图注释并在另一个视图中定义点击操作。