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 trialJudy Tsai
6,376 PointsGet 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() {
super.viewDidLoad()
self.locationManager = CLLocationManager()
locationManager.requestAlwaysAuthorization()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startMonitoringSignificantLocationChanges()
}
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
11,688 PointsIn iOS 8, there are 2 new requirements that needs to be done to enable location detection feature.
locationManager.requestAlwaysAuthorization() or requestWhenInUseAuthorization method
-
Add the following in your plist of your project file:
NSLocationAlwaysUsageDescription NSLocationWhenInUseUsageDescription
This should help you to retrieve the user's location.
Jason Wayne
11,688 PointsHmmm..interesting.
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!
Judy Tsai
6,376 PointsJudy Tsai
6,376 PointsThank 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:
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!!
Sahil Ambardekar
10,306 PointsSahil Ambardekar
10,306 PointsThis worked for me thanks! I only needed to use the NSLocationWhenInUseUsageDescription key for this app!