iosswiftmapkituserlocation

User Location is automatically called


In my code, I need to initialize my user Location: it works fine.

But whenever I want to change my location (by selecting an adress or sweeping on the screen) my app brings me back to my user Location.

I found a bit of an answer on that post. But, I don't call didUpdateUserLocation() . So I don't know what to do.

Does my app call frequently ViewDidLoad() ?

Here is my code, where I initialize my locations.

var usrLocation: CLLocationManager!

override func viewDidLoad() {
    super.viewDidLoad()
    if (CLLocationManager.locationServicesEnabled()) {
        usrLocation = CLLocationManager()
        usrLocation.delegate = self
        usrLocation.requestAlwaysAuthorization()
        usrLocation.requestLocation()
        usrLocation.startUpdatingLocation()
    }
    mapView.delegate = self
}

Also, I can not just delete my usrLocation.requestLocation() function, I need to center on my user at the app launching. I thought of calling it elsewhere, but I have no idea of where ?


Solution

  • So, as dan said, the problem was that I shouldn't call requestLocation() and startUpdatingLocation().

    So right now my code is like that, and it works perfectly:

    var usrLocation: CLLocationManager!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if (CLLocationManager.locationServicesEnabled()) {
            usrLocation = CLLocationManager()
            usrLocation.delegate = self
            usrLocation.requestAlwaysAuthorization()
            usrLocation.requestLocation()
        }
        mapView.delegate = self
    }