我希望在用户单击标注(calloutAccessoryControlTapped func)时打开详细信息视图。我将 rightCalloutAccessoryView 设置为我隐藏的按钮,然后在 calloutAccessoryControlTapped 内我想捕获事件并打开视图。
我已经尝试了很多我在互联网上找到的使用导航控制器的解决方案,但我无法让它工作。
import SwiftUI
import MapKit
struct MapView: UIViewRepresentable {
@ObservedObject var objMapData = MapModel.objMapModel
func makeCoordinator() -> Coordinator {
return MapView.Coordinator()
}
func makeUIView(context: Context) -> MKMapView {
let view = objMapData.mapView
view.showsUserLocation = true
view.delegate = context.coordinator
return view
}
func updateUIView(_ uiView: MKMapView, context: Context) {
}
class Coordinator: UIViewController, MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "AnnotationView")
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "AnnotationView")
}
if annotation.isKind(of: MKUserLocation.self){return nil}
else {
annotationView?.image = UIImage(named: "map-pin-not-selected")?.resize(35, 35)
}
let leftView = UIImageView(image: UIImage(named: "pump_left_blue"))
leftView.frame = CGRect(x: 0, y: 0, width: 55, height: 75)
leftView.backgroundColor = UIColor.red
annotationView?.leftCalloutAccessoryView = leftView
annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
annotationView?.rightCalloutAccessoryView?.frame = CGRectZero
annotationView?.canShowCallout = true
return annotationView
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
view.image = UIImage(named: "map-pin-selected")?.resize(35, 35)
}
func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
view.image = UIImage(named: "map-pin-not-selected")?.resize(35, 35)
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
// open an existing view, LocationDetailedView
}
}
}
struct LocationDetailedView: View {
var body: some View {
Text("Location details")
.foregroundColor(Color("colGrayText"))
.font(
.system(
size: 14,
weight: .bold,
design: .default
)
)
}
}