Learn Courses My Dashboard

I can't understand what this error means

Why not declare the locationManager as follows:

var locationManager = CLLocationManager()

rather than making it optional and then have an override init() method to set the delegate.

    override init() {

        //  init method of NSObject
        super.init()

        //  Set MapViewModell as the delegate of the location manager
        locationManager.delegate = self
    }

Then put this code:

inside your

func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {

and get rid of the function

private func checkLocationAuthorisation() { including the guard statement.

1 Like

Done…
but nothing changed

import MapKit
import SwiftUI

struct MapView: View {
    @State private var viewModel = MapViewModel()
    
    var body: some View {
        Map(coordinateRegion: $viewModel.region , showsUserLocation: true)
            .ignoresSafeArea()
            .accentColor(Color(.systemRed))
            .onAppear{
                viewModel.checkIfLocationServicesIsEnabled()
            }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        MapView()
    }
}
final class MapViewModel: NSObject,ObservableObject,CLLocationManagerDelegate{
    
    @Published var region = MKCoordinateRegion(
        center: CLLocationCoordinate2D(latitude: 31.209857,
                                       longitude: 29.958800),
        span: MKCoordinateSpan(latitudeDelta: 0.1,
                               longitudeDelta: 0.1))
    
    
    var locationManger = CLLocationManager()
    
    
    
    
    
    func checkIfLocationServicesIsEnabled() {
        if CLLocationManager.locationServicesEnabled() {
            locationManger = CLLocationManager()
                //            locationManger.delegate = self
        } else {
            print("turn it on ")
        }
    }
    
    
    override init() {
            //  init method of NSObject
        super.init()
        
            //  Set MapViewModell as the delegate of the location manager
        locationManger.delegate = self
    }
   
    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        
        switch locationManger.authorizationStatus{
                
            case .notDetermined:
                locationManger.requestWhenInUseAuthorization()
            case .restricted:
                print("your location is restricted likely due parental controls.")
            case .denied:
                print("you have denied this app location permission. go into settinhs to change it ")
            case .authorizedAlways, .authorizedWhenInUse:
                region = MKCoordinateRegion(center: locationManger.location!.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
            @unknown default:
                break
        }
        
        
    }
    
}


Ah sorry. Making those changes means that there is no need for the function:

func checkIfLocationServicesIsEnabled()

since locationManager is no longer Optional.

1 Like

Thanks a lot :heart: :heart: :heart: