From bf7175bdd62f6a6efa20aaec32f88f3942324ba6 Mon Sep 17 00:00:00 2001 From: Oliver Date: Fri, 16 May 2025 16:02:07 +0200 Subject: [PATCH] feat(UIKit): hide search view when annotation is selected --- StepMap/UIKitMapView.swift | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/StepMap/UIKitMapView.swift b/StepMap/UIKitMapView.swift index e4b73f0..105be8f 100644 --- a/StepMap/UIKitMapView.swift +++ b/StepMap/UIKitMapView.swift @@ -15,11 +15,15 @@ class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelega var viewModel: ViewModel var oldDirections: [MKRoute] = [] var oldDestination: MKMapItemAnnotation? + var detailsDisplayed = false private var cancellables = Set() + let searchViewConctroller: UIHostingController let mapView : MKMapView = { let map = MKMapView() map.showsUserTrackingButton = true map.showsUserLocation = true + map.selectableMapFeatures = .pointsOfInterest + map.pitchButtonVisibility = .adaptive return map }() @@ -27,6 +31,7 @@ class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelega init(locationManager: LocationManager, viewModel: ViewModel) { self.locationManager = locationManager self.viewModel = viewModel + self.searchViewConctroller = UIHostingController(rootView: SearchView(locationManager: locationManager, viewModel: viewModel)) super.init(nibName: nil, bundle: nil) } @@ -44,7 +49,7 @@ class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelega } override func viewDidAppear(_ animated: Bool) { - setSearchView() + showSearchView() } private func setLocation() { @@ -66,8 +71,7 @@ class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelega mapView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true } - private func setSearchView() { - let searchViewConctroller = UIHostingController(rootView: SearchView(locationManager: locationManager, viewModel: viewModel)) + private func showSearchView() { searchViewConctroller.view.backgroundColor = .clear searchViewConctroller.modalPresentationStyle = .pageSheet searchViewConctroller.edgesForExtendedLayout = [.top, .bottom, .left, .right] @@ -114,6 +118,18 @@ class UIKitMapView: UIViewController, MKMapViewDelegate, CLLocationManagerDelega return renderer } + func mapView(_ mapView: MKMapView, didSelect annotation: any MKAnnotation) { + hideSearchView() + } + + func mapView(_ mapView: MKMapView, didDeselect annotation: any MKAnnotation) { + showSearchView() + } + + func hideSearchView() { + searchViewConctroller.dismiss(animated: true) + } + private func bindViewModel() { viewModel.$directions .receive(on: DispatchQueue.main)