I am trying to create an application with only a map view that has a "Show user location" button placed on it, which I implement with the help of MapKit
. With a press of the button I would also like to get user coordinates and for that I employ CLLocationManagerDelegate
methods that I have trouble running. I did set "Privacy - Location When In Use Usage Description" string in plist.
Here is the code of my MapViewController
:
import UIKit
import MapKit
import CoreLocation
class MapViewController: UIViewController, CLLocationManagerDelegate {
// MARK: - Properties
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
var fetchedData = ""
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
// MARK: - Actions
// this puts user location on map
@IBAction func showUserLocation() {
// check for location tracing permissions first
let authStatus = CLLocationManager.authorizationStatus()
if authStatus == .notDetermined {
locationManager.requestWhenInUseAuthorization()
return
} else if authStatus == .denied || authStatus == .restricted {
showLocationServicesDeniedAlert()
return
}
let region = MKCoordinateRegion(center: mapView.userLocation.coordinate, latitudinalMeters: 1000, longitudinalMeters: 1000)
mapView.setRegion(mapView.regionThatFits(region), animated: true)
getCoordinates()
}
// MARK: - Helper methods
// handle permission errors
func showLocationServicesDeniedAlert() {
let alert = UIAlertController(title: "Paikannuspalvelut pois päältä", message: "Ole hyvä ja salli paikannuspalvelut tälle äpille.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(okAction)
present(alert, animated: true, completion: nil)
}
// get user coordinates
func getCoordinates() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
}
// MARK: - MKMapViewDelegate
extension MapViewController: MKMapViewDelegate {
}
// MARK: - CLLocationManagerDelegate
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("*** Location manager failed with error: \(error.localizedDescription)")
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let newLocation = locations.last!
print("*** didUpdateLocations \(newLocation)")
}