【发布时间】:2021-11-02 21:36:17
【问题描述】:
我正在使用 UIRepresentable 在地图上显示注释,并且希望能够在点击所选图钉时显示视图。
我之前使用Map(),所以可以使用.onTapGesture作为注释,但是现在注释是由UIKit制作的,我如何将选定的项目传递给主视图?
我之前的工作:
var body: some View {
ZStack {
Map(region: $region, annotationItems: $model.locations) { location in
MapPin(coordinate: location.coord)
.onTapGesture {
modelData.selectedLocation = location
modelData.isShowingDetail = true
}
}
if modelData.isShowingDetail {
DetailView(
isShowingDetail: $modelData.isShowingDetail,
location: modelData.selectedLocation!
)
}
}
}
现在我有了 UIViewRepresentable:
struct UIMapView: UIViewRepresentable {
// default setup - coordinator, makeUI, updateUI
class Coordinator: NSObject, MKMapViewDelegate {
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
// how to trigger the overlay??
}
}
}
任何帮助将不胜感激,因为我非常坚持这一点:)
【问题讨论】:
标签: swift swiftui mapkit mapkitannotation