1

I have a MapKit in my view, and I want to access the userLocation data. However, the actual function

func firstMapView(_ firstMapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation){
    print("it never gets called")
}

never ends up being called.

The whole code looks like this:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate{

    @IBOutlet weak var firstMapView: MKMapView!
    let locationManager = CLLocationManager()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        
        firstMapView.delegate = self
    }
    
    
    func firstMapView(_ firstMapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation){
        print("it never gets called")
    }
    
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == .authorizedWhenInUse {
            print("Authorized when in use detected")
            locationManager.startUpdatingLocation()
            firstMapView.showsUserLocation = true
        }
    }
}

When I run the code I get the debug message: print("Authorized when in use detected")

I saw this question, and tried to copy the things, but it didn't help.

zeytin
  • 5,545
  • 4
  • 14
  • 38
harcipulyka
  • 117
  • 1
  • 6
  • You're not using the actual delegate method: mapView(_:didUpdate:) – Don Oct 31 '20 at 16:19
  • Can you help me what the correct syntax would be then? I saw the Apple documentation but I wasn’t sure which part should I change and which ones not – harcipulyka Oct 31 '20 at 16:58

1 Answers1

1

Use this delegate function

func mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!) {
        print(userLocation)

    }

delete this one

func firstMapView(_ firstMapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation){
        print("it never gets called")
    }
zeytin
  • 5,545
  • 4
  • 14
  • 38
  • Thanks, that was the problem. I'll leave this link here to those, who might stumble across this thread later: https://learnappmaking.com/delegation-swift-how-to/ – harcipulyka Oct 31 '20 at 17:42