From c5b996ab9819a41540faa71a6dd30486d28221db Mon Sep 17 00:00:00 2001 From: Chris Mash Date: Thu, 8 Sep 2022 20:33:25 +0100 Subject: [PATCH 1/2] show user location on maps --- .../Maps/AllLocationsMapViewController.swift | 8 ++ .../Maps/MapLocationViewController.swift | 12 +++ iOSDevUK/Info.plist | 2 + .../Storyboards/MainStoryboard.storyboard | 97 ++++++++++--------- 4 files changed, 71 insertions(+), 48 deletions(-) diff --git a/iOSDevUK/Controllers/Maps/AllLocationsMapViewController.swift b/iOSDevUK/Controllers/Maps/AllLocationsMapViewController.swift index 8516c97..1cfdebe 100644 --- a/iOSDevUK/Controllers/Maps/AllLocationsMapViewController.swift +++ b/iOSDevUK/Controllers/Maps/AllLocationsMapViewController.swift @@ -36,6 +36,10 @@ class AllLocationsMapViewController: UIViewController, UITableViewDelegate, UITa override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) + + // In case this is the first time showing a map screen + // request permission to use the user's location + CLLocationManager().requestWhenInUseAuthorization() } // MARK: - Navigation @@ -90,6 +94,10 @@ class AllLocationsMapViewController: UIViewController, UITableViewDelegate, UITa } func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { + // Avoid overriding the user's location annotation view, use the default + guard !(annotation is MKUserLocation) else { + return nil + } let identifier = "iosdevukLocation" var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) diff --git a/iOSDevUK/Controllers/Maps/MapLocationViewController.swift b/iOSDevUK/Controllers/Maps/MapLocationViewController.swift index ef9b592..980a292 100644 --- a/iOSDevUK/Controllers/Maps/MapLocationViewController.swift +++ b/iOSDevUK/Controllers/Maps/MapLocationViewController.swift @@ -50,7 +50,19 @@ class MapLocationViewController: UIViewController, MKMapViewDelegate, SFSafariVi // Dispose of any resources that can be recreated. } + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + + // In case this is the first time showing a map screen + // request permission to use the user's location + CLLocationManager().requestWhenInUseAuthorization() + } + func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { + // Avoid overriding the user's location annotation view, use the default + guard !(annotation is MKUserLocation) else { + return nil + } let identifier = "iosdevukLocation" var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) diff --git a/iOSDevUK/Info.plist b/iOSDevUK/Info.plist index 2e71813..720a5f3 100644 --- a/iOSDevUK/Info.plist +++ b/iOSDevUK/Info.plist @@ -25,6 +25,8 @@ NSAllowsArbitraryLoads + NSLocationWhenInUseUsageDescription + The app will show your location on maps it presents UILaunchStoryboardName LaunchScreen UIMainStoryboardFile diff --git a/iOSDevUK/Storyboards/MainStoryboard.storyboard b/iOSDevUK/Storyboards/MainStoryboard.storyboard index 878118d..15a163e 100644 --- a/iOSDevUK/Storyboards/MainStoryboard.storyboard +++ b/iOSDevUK/Storyboards/MainStoryboard.storyboard @@ -2,6 +2,7 @@ + @@ -17,7 +18,7 @@ - + @@ -222,10 +223,10 @@ Information - + -