LocationManager.startUpdatingLocation finishes too late , can't get current location

132 views Asked by At

I am trying to change the initial zoom location of my MapView. I want the initialLocation to be (35,35) if myLocation == false and get the current Location if true. But startUpdatingLocation is finished after the initialLocation , so I cant get it and it crashes. If I use a breakpoint it works and gets the values.

if self.myLocation == false {
            self.initialLocation = CLLocation(latitude: 35.00, longitude: 35.00) }
      else {
            locManager.delegate = self
            locManager.desiredAccuracy = kCLLocationAccuracyBest
            checkLocationAuthorizationStatus()
            locManager.startUpdatingLocation()

      self.initialLocation = CLLocation(latitude: locManager.location.coordinate.longitude, longitude: locManager.location.coordinate.longitude) 
            }

        centerMapOnLocation(self.initialLocation)
}
1

There are 1 answers

0
rmp On

You need to use the locationManager delegate: locationManager:didUpdateLocations to set the location.
Place your self.initialLocation= code in the delegate.

Then create another function that you can call to execute your centerMapOnLocation(self.initialLocation) code.