Skip to content

Commit

Permalink
Merge pull request #4 from LucasJorgeHubert/feature/add-mapview-annot…
Browse files Browse the repository at this point in the history
…ation

Feature - Add mapview annotation
  • Loading branch information
LucasJorgeHubert authored May 18, 2023
2 parents 42f0d55 + 7f1f2ec commit 2dd5c34
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions MarvelUI/Core/Home/View/UberMapViewRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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(
Expand All @@ -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)
}
}
}

0 comments on commit 2dd5c34

Please sign in to comment.