diff --git a/StepMap.xcodeproj/project.xcworkspace/xcuserdata/oliverhnat.xcuserdatad/UserInterfaceState.xcuserstate b/StepMap.xcodeproj/project.xcworkspace/xcuserdata/oliverhnat.xcuserdatad/UserInterfaceState.xcuserstate index dd5d159..39485ba 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 new file mode 100644 index 0000000..3d6f807 --- /dev/null +++ b/StepMap/AnnotationView.swift @@ -0,0 +1,36 @@ +// +// AnnotationView.swift +// StepMap +// +// Created by Oliver Hnat on 16/05/2025. +// + +import SwiftUI +import MapKit + +struct AnnotationView: View { + var pm: CLPlacemark + var body: some View { + ZStack { + Rectangle() + .fill(.thinMaterial) + .ignoresSafeArea() + VStack { + Text(pm.locality ?? "") + Text(pm.name ?? "") + Text("Name: \(pm.name ?? "")") + Text("Street: \(pm.thoroughfare ?? "")") + Text("City: \(pm.locality ?? "")") + Text("Postal Code: \(pm.postalCode ?? "")") + Text("Country: \(pm.country ?? "")") + ForEach(pm.areasOfInterest ?? [], id: \.self) { area in + Text("Area: \(area)") + } + if let coordinate = pm.location?.coordinate { + let mkPlacemark = MKPlacemark(coordinate: coordinate) + Image(systemName: Defaults.getIconFor(pointOfInterest: MKMapItem(placemark: mkPlacemark).pointOfInterestCategory)) + } + } + } + } +} diff --git a/StepMap/UIKitMapView.swift b/StepMap/UIKitMapView.swift index 105be8f..4d19ea0 100644 --- a/StepMap/UIKitMapView.swift +++ b/StepMap/UIKitMapView.swift @@ -18,6 +18,7 @@ class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelega var detailsDisplayed = false private var cancellables = Set() let searchViewConctroller: UIHostingController + var annotationViewController: UIHostingController? let mapView : MKMapView = { let map = MKMapView() map.showsUserTrackingButton = true @@ -120,6 +121,38 @@ class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelega func mapView(_ mapView: MKMapView, didSelect annotation: any MKAnnotation) { hideSearchView() + hideAnnotationView() + showAnnotation(annotation: annotation) + } + + func showAnnotation(annotation: MKAnnotation) { + 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)) + if let avc = self.annotationViewController { + avc.view.backgroundColor = .clear + avc.modalPresentationStyle = .pageSheet + avc.edgesForExtendedLayout = [.top, .bottom, .left, .right] + if let sheet = avc.sheetPresentationController { + let smallDetentId = UISheetPresentationController.Detent.Identifier("small") + let smallDetent = UISheetPresentationController.Detent.custom(identifier: smallDetentId) { context in + return 350 + } + sheet.detents = [smallDetent, .large()] + sheet.largestUndimmedDetentIdentifier = .large + sheet.prefersScrollingExpandsWhenScrolledToEdge = false + sheet.prefersGrabberVisible = true + sheet.prefersEdgeAttachedInCompactHeight = true + sheet.widthFollowsPreferredContentSizeWhenEdgeAttached = true + } + self.present(avc, animated: true, completion: nil) + } + } + } func mapView(_ mapView: MKMapView, didDeselect annotation: any MKAnnotation) { @@ -129,6 +162,11 @@ class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelega func hideSearchView() { searchViewConctroller.dismiss(animated: true) } + func hideAnnotationView() { + if let avc = self.annotationViewController { + avc.dismiss(animated: true) + } + } private func bindViewModel() { viewModel.$directions