0

I'm not sure of the problem here, but I'll post my code below. Basically, I had MapKit running fine in xcode 12, but after the update to xcode 13, MapKit still shows a map; however, does not show a user location anymore. I read the new xcode13 documentation but haven't seen anything about MapKit updates so I can't figure out why anything would change, but would greatly appreciate any help.

import UIKit
import MapKit
import CoreLocation

class MapViewController: UIViewController, CLLocationManagerDelegate {
    @IBOutlet var sideMenuBtn: UIBarButtonItem!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "Map"

        //menu btn tint color
        navigationController?.navigationBar.tintColor = .white
        //Nav bar color
        navigationController?.navigationBar.barTintColor = .black
        
        sideMenuBtn.target = self.revealViewController()
        sideMenuBtn.action = #selector(self.revealViewController()?.revealSideMenu)
        
        
        setupMapView()
        checkLocationServices()
        if #available(iOS 14.0, *) {
            overrideUserInterfaceStyle = .dark
        }
    }
    
    //Declaring the map view
    let mapView = MKMapView()
    
    //Setting up mapview constraints
    func setupMapView() {
            view.addSubview(mapView)
            mapView.translatesAutoresizingMaskIntoConstraints = false
            mapView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
            mapView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
            mapView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor).isActive = true
            mapView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor).isActive = true
    }
    
    let locationManager = CLLocationManager()
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            guard let location = locations.last else { return }
            let region = MKCoordinateRegion.init(center: location.coordinate, latitudinalMeters: 4000, longitudinalMeters: 4000)
            mapView.setRegion(region, animated: true)
    }
       
    func checkLocationAuthorization() {
            switch CLLocationManager.authorizationStatus() {
            case .authorizedWhenInUse:
                mapView.showsUserLocation = true
                followUserLocation()
                locationManager.startUpdatingLocation()
                break
            case .denied:
                // Show alert
                break
            case .notDetermined:
                locationManager.requestWhenInUseAuthorization()
            case .restricted:
                // Show alert
                break
            case .authorizedAlways:
                break
            @unknown default:
                break
            }
    }
        
    func checkLocationServices() {
            if CLLocationManager.locationServicesEnabled() {
                setupLocationManager()
                checkLocationAuthorization()
            } else {
                // the user didn't turn it on
            }
    }
        
    func followUserLocation() {
        if let location = locationManager.location?.coordinate {
            let region = MKCoordinateRegion.init(center: location, latitudinalMeters: 4000, longitudinalMeters: 4000)
            mapView.setRegion(region, animated: true)
        }
    }
        
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        checkLocationAuthorization()
    }
        
    func setupLocationManager() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
    }
}
  • What you're doing seems confused. If you simply want to display the user's location on a map view, there is no need to call `startUpdatingLocation`. And you shouldn't be messing with the map view's region; you are fighting with the map view. – matt Oct 06 '21 at 15:42
  • I called `startUpdatingLocation` so user location is constantly updated as the user moves. Is that not needed? – bigyoshi Oct 06 '21 at 15:45
  • Yes, it is not needed. The map will just follow the user (based on the user tracking mode). – matt Oct 06 '21 at 15:47

0 Answers0