Cod3rMax
Cod3rMax

Reputation: 958

Failing to display user location SwiftUI

Before i post my code, i should tell you that my code run on a project and on a second project doesn't run!

The error that I get is:

[MKCoreLocationProvider] CLLocationManager(<CLLocationManager: 0x280cfc050>) for <MKCoreLocationProvider: 0x283cd0870> did fail with error: Error Domain=kCLErrorDomain Code=1 "(null)"

This is really wired why my code run on a project and the project that I'm working on it right now doesn't! and I'm using the same version ios 14.

  1. MapView
import Foundation
import UIKit
import SwiftUI
import MapKit
    
struct MapView: UIViewRepresentable {
        
 typealias UIViewType = UIView
        
        
  func makeCoordinator() -> Coordinator {
            Coordinator(self)
  }
             
  func makeUIView(context: Context) -> UIView {
            let map = MKMapView()
            
            map.delegate = context.coordinator
            map.showsUserLocation = true
            return map
  }
        
  func updateUIView(_ uiView: UIView, context: Context) {
            
  }
              
}
  1. Coordinator
import Foundation
import MapKit

class Coordinator: NSObject, MKMapViewDelegate {
        
 var control: MapView
        
 init(_ control: MapView) {
            self.control = control
 }
        
        
}
  1. Location manager
class GetUserLocation: NSObject, ObservableObject, CLLocationManagerDelegate {
        
 let locationManager = CLLocationManager()
 @Published var location: CLLocation? = nil
        
 override init() {
  super.init()
            
  self.locationManager.delegate = self
  self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
  self.locationManager.distanceFilter = kCLDistanceFilterNone
  self.locationManager.requestWhenInUseAuthorization()
  self.locationManager.startUpdatingLocation()
            
 }
        
        
  func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
           
            guard let location = locations.last else {
                return
            }
            
            self.location = location
  }
        
        
}

Upvotes: 0

Views: 578

Answers (1)

Cod3rMax
Cod3rMax

Reputation: 958

After having some rest, I started debugging the problem I found out that the init() function it's not getting called in file 3. Location manager.

I tried to initialize the object from other files and guess what it did work without any problem, another wired thing here!

So the init() function is getting called from other files however in 4. UserLocationMapView it doesn't get called and i couldn't understand why!

After having a cup of coffe I found out that in SceneDelegate I was calling the wrong view file, I had UserLocationMapView that display the map, however I was calling 3. MapView in SceneDelegate.

That's a silly mistake, when you call both files in SceneDelegate, they display the map without a problem. So when you see the map being displayed in the simulator you will think that it's working, however one of them call the init() function and one of them doesn't have it.

Upvotes: 2

Related Questions