Frankly saying, your code is broken in many ways and I do not see what you want to do with this code.
Your ViewController has 3 instance methods -- viewDidLoad(), locationManager(_:didUpdateLocations:didFailWithError:) and addUserAnnotation(press:)
But it does not have a method locationManager(_:didUpdateLocations:) which the error message is claiming.
Two methods are nested inside the method locationManager(_:didUpdateLocations:didFailWithError:), which seems to be a method CLLocationManagerDelegate, so they should not be nested.
You should bette care about proper indentation.
Keep using Select All(Cmd-A) and Re-Indent (Ctrl-I). You will find some of the func are indented deeper.
And this is not critical, but you are using Capitalized names such as MyMapView or LocationManager. That is making your code very, very, very hard to read.
Your ViewController should look like:
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]) {
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)
//User Location Pin
let myPin = MKPointAnnotation()
myPin.coordinate = userCoordinates
myPin.title = "Pin"
myPin.subtitle = "Subtitle"
myMapView.addAnnotation(myPin)
}
}
//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 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)
}
}
}
You need to define the method locationManager(_:didUpdateLocations:), not locationManager(_:didUpdateLocations:didFailWithError:).
Are you using some sort of tutorials or textbooks? Do care about every single letter and symbol.
I haven't checked the content of each method, but at lease my code above will not cause Delegate must respond to locationManager:didUpdateLocations:.