Post

Replies

Boosts

Views

Activity

MapKit: "Delegate must respond to locationManager:didUpdateLocations:"
Hi, I've been trying to develop an app that allows a user to drop a pin on the map once they've long pressed on it. When trying to launch the app it throws a "Thread 1: "Delegate must respond to locationManager:didUpdateLocations:" error. I would appreciate if you could help me locate the source of the issue and suggest a fix. This is my first app and I am still not entirely sure what I am doing most of the time. ;) Here's the code: // //  ViewController.swift //  GetUserLocation // //  Created by Jakub Rakowski on 22/03/2021. // import UIKit import MapKit class ViewController: UIViewController , CLLocationManagerDelegate {     @IBOutlet weak var MyMapView: MKMapView!     let LocationManager = CLLocationManager ()     override func viewDidLoad() {         super.viewDidLoad()         // Do any additional setup after loading the view.                  LocationManager.delegate = self         LocationManager.desiredAccuracy = kCLLocationAccuracyBest         if(CLLocationManager.locationServicesEnabled()) {             LocationManager.requestLocation()             LocationManager.startUpdatingLocation()         }     } //User Location     func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation], didFailWithError error: Error) {         if let UserLocation = locations.first {             manager.stopUpdatingLocation()             let userCoordinates = CLLocationCoordinate2D(latitude: LocationManager.location?.coordinate.latitude ?? 0.0, longitude: LocationManager.location?.coordinate.longitude ?? 0.0)             let Span = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)             let Region = MKCoordinateRegion(center: userCoordinates, span: Span)             MyMapView.setRegion(Region, animated: true) //Requesting Location Authorization                 func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {                     switch manager.authorizationStatus {                     case .authorizedAlways:                         return                     case .authorizedWhenInUse:                         return                     case .denied:                         return                     case .restricted:                         LocationManager.requestWhenInUseAuthorization()                     case .notDetermined:                         LocationManager.requestWhenInUseAuthorization()                     default:                         LocationManager.requestWhenInUseAuthorization()                     }                 }                 func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {                     print(error)}     //User Location Pin             let MyPin = MKPointAnnotation()             MyPin.coordinate = userCoordinates             MyPin.title = "Pin"             MyPin.subtitle = "Subtitle"             MyMapView.addAnnotation(MyPin)         }}              //User Generated Pin     @objc func addUserAnnotation(press:UILongPressGestureRecognizer){                 if press.state == .began                 {                     let location = press.location(in: self.MyMapView)                     let coordinates = MyMapView.convert(location, toCoordinateFrom: MyMapView)                            let annotation = MKPointAnnotation()                     annotation.coordinate = coordinates                        annotation.title = "LongPress Annotation"                          MyMapView.addAnnotation(annotation)                            let uilpgr = UILongPressGestureRecognizer (target: self, action: #selector (addUserAnnotation(press:)))                                     uilpgr.minimumPressDuration = 1                         MyMapView.addGestureRecognizer(uilpgr)                                  addUserAnnotation(press: uilpgr)                                      }                              }      }                 
2
0
2.9k
Mar ’21