diff --git a/StepMap.xcodeproj/project.xcworkspace/xcuserdata/oliverhnat.xcuserdatad/UserInterfaceState.xcuserstate b/StepMap.xcodeproj/project.xcworkspace/xcuserdata/oliverhnat.xcuserdatad/UserInterfaceState.xcuserstate index 39485ba..08614d8 100644 Binary files a/StepMap.xcodeproj/project.xcworkspace/xcuserdata/oliverhnat.xcuserdatad/UserInterfaceState.xcuserstate and b/StepMap.xcodeproj/project.xcworkspace/xcuserdata/oliverhnat.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/StepMap/AnnotationView.swift b/StepMap/AnnotationView.swift index 3d6f807..46db5f7 100644 --- a/StepMap/AnnotationView.swift +++ b/StepMap/AnnotationView.swift @@ -10,12 +10,30 @@ import MapKit struct AnnotationView: View { var pm: CLPlacemark + var title: String? + var coordinate: CLLocation + @ObservedObject var viewModel: ViewModel var body: some View { ZStack { Rectangle() .fill(.thinMaterial) .ignoresSafeArea() - VStack { + VStack(alignment: .leading) { + HStack(alignment: .top) { + Text((title ?? pm.areasOfInterest?.first ?? pm.name) ?? "\(coordinate.coordinate.latitude.description)ยบ, \(coordinate.coordinate.longitude.description)") + .font(.title) + .bold() + Spacer() + Button(action: { + viewModel.showDetails = false + + }, label: { + Image(systemName: "multiply.circle") + }) + } + .padding(.horizontal) + .padding(.top, 20) + Spacer() Text(pm.locality ?? "") Text(pm.name ?? "") Text("Name: \(pm.name ?? "")") @@ -31,6 +49,8 @@ struct AnnotationView: View { Image(systemName: Defaults.getIconFor(pointOfInterest: MKMapItem(placemark: mkPlacemark).pointOfInterestCategory)) } } + .frame(maxWidth: .infinity) + .ignoresSafeArea() } } } diff --git a/StepMap/UIKitMapView.swift b/StepMap/UIKitMapView.swift index 4d19ea0..f93ffe4 100644 --- a/StepMap/UIKitMapView.swift +++ b/StepMap/UIKitMapView.swift @@ -15,7 +15,6 @@ class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelega var viewModel: ViewModel var oldDirections: [MKRoute] = [] var oldDestination: MKMapItemAnnotation? - var detailsDisplayed = false private var cancellables = Set() let searchViewConctroller: UIHostingController var annotationViewController: UIHostingController? @@ -126,13 +125,13 @@ class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelega } func showAnnotation(annotation: MKAnnotation) { + viewModel.showDetails = true let location = CLLocation(latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude) CLGeocoder().reverseGeocodeLocation(location) { placemarks, error in guard let placemark = placemarks?.first, error == nil else { return } - print(placemark) - self.annotationViewController = UIHostingController(rootView: AnnotationView(pm: placemark)) + self.annotationViewController = UIHostingController(rootView: AnnotationView(pm: placemark, title: annotation.title as? String, coordinate: location, viewModel: self.viewModel)) if let avc = self.annotationViewController { avc.view.backgroundColor = .clear avc.modalPresentationStyle = .pageSheet @@ -156,7 +155,9 @@ class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelega } func mapView(_ mapView: MKMapView, didDeselect annotation: any MKAnnotation) { - showSearchView() + if viewModel.showDetails { + viewModel.showDetails = false + } } func hideSearchView() { @@ -175,6 +176,17 @@ class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelega self?.refreshRoute() } .store(in: &cancellables) + viewModel.$showDetails + .receive(on: DispatchQueue.main) + .sink { [weak self] value in + print(value) + if !value { + self?.hideAnnotationView() + self?.showSearchView() +// self?.mapView.selectedAnnotations = [] + } + } + .store(in: &cancellables) } } diff --git a/StepMap/ViewModel.swift b/StepMap/ViewModel.swift index 6bc8d86..913b933 100644 --- a/StepMap/ViewModel.swift +++ b/StepMap/ViewModel.swift @@ -13,6 +13,8 @@ class ViewModel: ObservableObject { @Published var directions: [MKRoute] = [] @Published var stepLength: Double? @Published var destination: MKMapItem? + @Published var showDetails = false + func saveValue(_ value: String) { UserDefaults.standard.set(value, forKey: "test")