-5

I am using a real device to get my current location the problem is the locationManager.location is nil and the function didUpdateLocations is not called.

var location = CLLocationManager()
@IBOutlet weak var map: MKMapView!

override func viewDidLoad() {
    super.viewDidLoad()
    map.showsPointsOfInterest = true
    map.showsScale = true
    map.showsUserLocation = true

    locationManagerConfiguration()
}
func locationManagerConfiguration(){
    location.requestAlwaysAuthorization()
    location.requestWhenInUseAuthorization()
    if CLLocationManager.locationServicesEnabled(){
        location.delegate = self
        location.desiredAccuracy = kCLLocationAccuracyBest
        location.startUpdatingLocation()
    }

    let sourceCoordinates = location.location?.coordinate
    let sourcePlacemark = MKPlacemark(coordinate: sourceCoordinates! 

here is the problem the sourceCoordinate is nil

Bhavesh Nayi
  • 3,626
  • 1
  • 27
  • 42
Alaa Mamdouh
  • 3
  • 1
  • 4
  • 1
    Hello there. You are likely to see a lot of downvotes on your question until you add a lot more detail. We will need to see the relevant code from your project to be able to give you useful advice. Please see this StackOverflow article on posting [Minimal, Complete, Verifiable Questions](https://stackoverflow.com/help/mcve) – Mike Hay Apr 26 '19 at 12:10
  • I posted my GitHub link to let everyone see my code and solve the problem – Alaa Mamdouh Apr 26 '19 at 12:13
  • 1
    No one wants to click on a link and see all your code. Your job is to figure out what is key to your issue and show us, right here. – matt Apr 26 '19 at 12:15

1 Answers1

0

The code you have provided doesn't match the statement in your question, but I think I see the issue in the code. According to the apple docs: Startupdatinglocation

location.startUpdatingLocation()

Is an async function that takes several seconds to obtain a location. The function will return immediately, but the delegate callback will be called when the system has obtained a GPS location (several seconds later, asynchronously). Your code above is calling startUpdatingLocation() and immediately expecting a value to be present (synchronously, which it will never be). Your code needs to be more like:

func locationManagerConfiguration(){
    location.requestAlwaysAuthorization()
    location.requestWhenInUseAuthorization()
    if CLLocationManager.locationServicesEnabled(){
        location.delegate = self
        location.desiredAccuracy = kCLLocationAccuracyBest
        location.startUpdatingLocation()
    }
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    // TODO: Check the array is not empty
    let sourceCoordinates = locations[0]
    let sourcePlacemark = MKPlacemark(coordinate: sourceCoordinates!)
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    print("Location error: \(error)")
}
Bhavesh Nayi
  • 3,626
  • 1
  • 27
  • 42
Simon McLoughlin
  • 8,293
  • 5
  • 32
  • 56
  • 1
    Furthermore, on the first execution, the two requests for location services will also run asynchronously, and no location will be provided to the app until the user grants permission. – Steve Madsen Apr 26 '19 at 14:03