0

I am trying to get the user's location in Swift. Using the code below. But when I call

let currentloc = self.locationManager.location?.coordinate , it returns a nil value. The code below is above the let currentloc line in the script.

     self.locationManager.delegate = self
    self.locationManager.requestAlwaysAuthorization()
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
gareth power
  • 209
  • 2
  • 4
  • 12
  • 1
    Is the delegate method called? Because the location is not "immediate". You have to retrieve it through the delegate method. – Larme Jun 03 '16 at 14:03
  • I'm not sure. what is the delegate method? – gareth power Jun 03 '16 at 14:04
  • 3
    `self.locationManager.delegate`: Why did you put this line? ` - locationManager:didUpdateLocations:` – Larme Jun 03 '16 at 14:07
  • Oh I see, but the problem is that I am returning labels with the distance that each place is from the user, so I dont know how to work it – gareth power Jun 03 '16 at 14:19

2 Answers2

1

Make sure you:

  1. import Corelocation + import MapKit in your class.
  2. conform to the MKMapViewDelegate and CLLocationManagerDelegate
  3. instantiate the locationmanager: let locationManager = CLLocationManager() in your class variables
  4. in your viewDidLoad try:

    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager. requestAlwaysAuthorization()
    self.locationManager.startUpdatingLocation()
    self.mapView.showsUserLocation = true
    
  5. Add the following delegate method to print your location:

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    print(locations.last!.coordinate)
    }
    
Hapeki
  • 2,153
  • 1
  • 20
  • 36
  • Thank you. But I think it is a problem regarding updating user location and how it isnt instant. See, I have a loop that prints labels with their distance from the user, soI am trying to make it instant. it doesn't seem to work – gareth power Jun 03 '16 at 14:17
  • Are you using a simulator or a real device? – Hapeki Jun 03 '16 at 14:19
  • A simulator (iPhone 6) – gareth power Jun 03 '16 at 14:19
  • Simulators don't work with current locations, you have to set them manually. Read into this: http://stackoverflow.com/a/16082525/6414904 – Hapeki Jun 03 '16 at 14:23
0

TLDR;

  • Product -> Scheme -> Edit Scheme -> Options -> Allow Location Simulation must be checked and try providing a default location, don't leave it set to "none"

When you test on simulator, you have to provide a default location, please check my answer here : https://stackoverflow.com/a/32887820/4887400

Community
  • 1
  • 1
H4Hugo
  • 2,570
  • 3
  • 16
  • 32
  • Thanks, but does this mean that my code will work on a device when I call 'locationManager.location.coordinates' and have my code using the location straight after it? Or would I have to call the rest of the code in the locationManager delegate?? – gareth power Jun 03 '16 at 15:39