From d342a564a26c7f5598ea123245854c4092874219 Mon Sep 17 00:00:00 2001 From: Lucas Jorge Hubert Date: Wed, 17 May 2023 17:59:27 -0300 Subject: [PATCH 1/2] add annotation feature --- .../Home/View/UberMapViewRepresentable.swift | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/MarvelUI/Core/Home/View/UberMapViewRepresentable.swift b/MarvelUI/Core/Home/View/UberMapViewRepresentable.swift index 0d13b2e..fc9c60a 100644 --- a/MarvelUI/Core/Home/View/UberMapViewRepresentable.swift +++ b/MarvelUI/Core/Home/View/UberMapViewRepresentable.swift @@ -25,6 +25,7 @@ struct UberMapViewRepresentable: UIViewRepresentable { func updateUIView(_ uiView: UIViewType, context: Context) { if let coordinate = locationViewModel.selectedLocationCoordinate { print("DEBUG: Selected coordinate in map view \(coordinate)") + context.coordinator.addAndSelectAnnotation(withCoordinate: coordinate) } } @@ -36,13 +37,19 @@ struct UberMapViewRepresentable: UIViewRepresentable { extension UberMapViewRepresentable { class MapCoordinator: NSObject, MKMapViewDelegate { + + // MARK: - Properties let parent: UberMapViewRepresentable + // MARK: - Lifecycle + init(parent: UberMapViewRepresentable) { self.parent = parent super.init() } + // MARK: - MKMapViewDelegate + func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) { let region = MKCoordinateRegion( center: CLLocationCoordinate2D( @@ -54,5 +61,15 @@ extension UberMapViewRepresentable { parent.mapView.setRegion(region, animated: true) } + + // MARK: - Helpers + + func addAndSelectAnnotation(withCoordinate coordinate: CLLocationCoordinate2D) { + parent.mapView.removeAnnotations(parent.mapView.annotations) + let anno = MKPointAnnotation() + anno.coordinate = coordinate + self.parent.mapView.addAnnotation(anno) + self.parent.mapView.selectAnnotation(anno, animated: true) + } } } From 7f1f2ece41f4b78063d327eab27aa552b5784003 Mon Sep 17 00:00:00 2001 From: Lucas Jorge Hubert Date: Thu, 18 May 2023 10:52:55 -0300 Subject: [PATCH 2/2] Add zoom animation --- MarvelUI/Core/Home/View/UberMapViewRepresentable.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/MarvelUI/Core/Home/View/UberMapViewRepresentable.swift b/MarvelUI/Core/Home/View/UberMapViewRepresentable.swift index fc9c60a..cebad5e 100644 --- a/MarvelUI/Core/Home/View/UberMapViewRepresentable.swift +++ b/MarvelUI/Core/Home/View/UberMapViewRepresentable.swift @@ -70,6 +70,8 @@ extension UberMapViewRepresentable { anno.coordinate = coordinate self.parent.mapView.addAnnotation(anno) self.parent.mapView.selectAnnotation(anno, animated: true) + + parent.mapView.showAnnotations(parent.mapView.annotations, animated: true) } } }