feat(LocationView): show basic information about a selected location
This commit is contained in:
		
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										36
									
								
								StepMap/AnnotationView.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								StepMap/AnnotationView.swift
									
									
									
									
									
										Normal file
									
								
							| @@ -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)) | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -18,6 +18,7 @@ class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelega | |||||||
|     var detailsDisplayed = false |     var detailsDisplayed = false | ||||||
|     private var cancellables = Set<AnyCancellable>() |     private var cancellables = Set<AnyCancellable>() | ||||||
|     let searchViewConctroller: UIHostingController<SearchView> |     let searchViewConctroller: UIHostingController<SearchView> | ||||||
|  |     var annotationViewController: UIHostingController<AnnotationView>? | ||||||
|     let mapView : MKMapView = { |     let mapView : MKMapView = { | ||||||
|         let map = MKMapView() |         let map = MKMapView() | ||||||
|         map.showsUserTrackingButton = true |         map.showsUserTrackingButton = true | ||||||
| @@ -120,6 +121,38 @@ class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelega | |||||||
|      |      | ||||||
|     func mapView(_ mapView: MKMapView, didSelect annotation: any MKAnnotation) { |     func mapView(_ mapView: MKMapView, didSelect annotation: any MKAnnotation) { | ||||||
|         hideSearchView() |         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) { |     func mapView(_ mapView: MKMapView, didDeselect annotation: any MKAnnotation) { | ||||||
| @@ -129,6 +162,11 @@ class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelega | |||||||
|     func hideSearchView() { |     func hideSearchView() { | ||||||
|         searchViewConctroller.dismiss(animated: true) |         searchViewConctroller.dismiss(animated: true) | ||||||
|     } |     } | ||||||
|  |     func hideAnnotationView() { | ||||||
|  |         if let avc = self.annotationViewController { | ||||||
|  |             avc.dismiss(animated: true) | ||||||
|  |         } | ||||||
|  |     } | ||||||
|      |      | ||||||
|     private func bindViewModel() { |     private func bindViewModel() { | ||||||
|         viewModel.$directions |         viewModel.$directions | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Oliver
					Oliver