diff --git a/MarvelUI/Core/Home/View/UberMapViewRepresentable.swift b/MarvelUI/Core/Home/View/UberMapViewRepresentable.swift index 0d13b2e..cebad5e 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,17 @@ 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) + + parent.mapView.showAnnotations(parent.mapView.annotations, animated: true) + } } }