Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Weather App with Swift (Retired) Displaying Our Weather Data The End!

Judy Tsai
Judy Tsai
6,376 Points

Get latitude and longitude for current location using CLLocationManager

I am trying to use CLLocationManager to get the latitude and longitude for current location but I am having very difficult time with this for whatever reason.

I Googled and found example (http://stackoverflow.com/questions/25296691/swift-get-users-current-location-coordinates) like this:

    var locationManager: CLLocationManager!

    override func viewDidLoad() {
       self.locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
   func locationManager(manager: CLLocationManager!,   didUpdateLocations locations: [AnyObject]!) {
          var locValue:CLLocationCoordinate2D = manager.location.coordinate
         println("locations = \(locValue.latitude) \(locValue.longitude)")

I also tried @mamat golo's code in his post: https://teamtreehouse.com/forum/how-can-i-pass-the-latitude-and-longitude-data-from-cllocationmanager-delegate-to-forecasturl

But I can't seem to create any log (println) at all. It's like it never even enter locationManager function I am confused here.

  • Am I supposed to call the locationManager function somewhere to get the coordinate?
  • I tried to run the function as locationManager.locationManager() but they have the same name and caused error.
  • In Mamat's example, it passed two locations: new and old. I've read that CLLocationManager is mainly to determine location value when user is moving and not the current location. If so, what should I use? Or what should be the value for the newLocation and oldLocation?

I spent several hours last night, read the developer documents but I am still pretty lost here. Can someone please help me a little on this?

Much appreciated.

2 Answers

Jason Wayne
Jason Wayne
11,688 Points

In iOS 8, there are 2 new requirements that needs to be done to enable location detection feature.

  1. locationManager.requestAlwaysAuthorization() or requestWhenInUseAuthorization method

  2. Add the following in your plist of your project file:


This should help you to retrieve the user's location.

Judy Tsai
Judy Tsai
6,376 Points

Thank you, Jason,

I tried that but unfortunately it didn't work.

However, I think I am making some progress here. I revised my code with reference from this post on stackoverflow: http://stackoverflow.com/questions/24063798/cllocation-manager-in-swift-to-get-location-of-user

Now I can get more detail error message explanation. The error message returns:

error to get location : Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)"

I looked up some solutions on Google again and tried the suggestion listed here: http://stackoverflow.com/questions/1409141/location-manager-error-kclerrordomain-error-0

But I still have the issue.

So now I have another theory:

1) I was testing this on the simulator because I do not have iPhone 6 in hand. Couldn't we test location manager on simulator or was there another setting I missed?

2) I tried to downgrade the app to for iOS 7 so I can test it on my phone. And now it stuck at black loading screen with this error:

2014-11-07 23:38:24.664 Stormy[1296:60b] -[CLLocationManager requestAlwaysAuthorization]: unrecognized selector sent to instance 0x1456f960
2014-11-07 23:38:24.669 Stormy[1296:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CLLocationManager requestAlwaysAuthorization]: unrecognized selector sent to instance 0x1456f960'
*** First throw call stack:
(0x2e2bcf4b 0x386fd6af 0x2e2c08e7 0x2e2bf1cb 0x2e20e4d8 0x2a650 0x2abe8 0x30a3437b 0x30a34139 0x30b522bb 0x30abb8d7 0x30bbb223 0x30a64a85 0x30b9d567 0x30ae0ba5 0x30ae09d1 0x30ae0993 0x30ae0955 0x3649c 0x36850 0x30aa02ff 0x30a9fd4f 0x30a9a353 0x30a3541f 0x30a34721 0x30a99b3d 0x32f1870d 0x32f182f7 0x2e2879df 0x2e28797b 0x2e28614f 0x2e1f0c27 0x2e1f0a0b 0x30a98dd9 0x30a94049 0x36f94 0x36fd0 0x38c05ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException

Unfortunately I don't have enough space on my phone to upgrade the iOS.

Is that the only way to test it? Or is it another way to get the simulator to work? Or am I just missing something here?

Thank you!!

This worked for me thanks! I only needed to use the NSLocationWhenInUseUsageDescription key for this app!

Jason Wayne
Jason Wayne
11,688 Points


When you downgrade the version, that code would not work. Because CLLocationManager method for requestAlwaysAuthorization or requestWhenInUseAuthorization are for iOS8. Hence, the error stating that it is an unrecognised selector.

For the issue about testing the location on simulator. For it to work on a simulator, you have to specify the location coordinates. Simulator doesn't have the ability to track your user location. To set the coordinates, using your Simulator, go under the Debug menu, and under Location, select Custom Location, which you would specify your coordinates. That should get it to work.

Hope that would help you out. Cheers!