0

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 ?

Community
  • 1
  • 1
  • 1
    Don't call both `requestLocation()` and `startUpdatingLocation()`. Call `requestLocation()` if you just want to get the user's current location once and `startUpdatingLocation()` if you want continuous location updates. – dan Aug 03 '16 at 15:28
  • Oh, it was that. Thank's a lot. You should put it in a true answer :) – Adèle Dunand Aug 03 '16 at 15:33

2 Answers2

1

you have implemented
usrLocation.startUpdatingLocation()

This is keep on updating location, so as soon you point to another location, it updates the user location.

Remove this line the code will work.

Surbhi Garg
  • 414
  • 5
  • 19
0

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
}