Thanks to the Core Location framework we can get the current location of our device. We need to create a CLLocationManager object and start it. Its delegate method will return CLLocation objects which among other items will contain the longitude and latitude of our device.
Here’s how to do it. Let’s create the manager as a property (self.manager) and initialise it with this custom method:
[emember_protected]
1 2 3 4 5 6 7 8 9 |
- (CLLocationManager *)manager { if (!_manager) { _manager = [[CLLocationManager alloc]init]; _manager.delegate = self; _manager.desiredAccuracy = kCLLocationAccuracyBest; } return _manager; } |
Then we start the manager:
1 |
[self.manager startUpdatingLocation]; |
We can also stop the manager when we no longer need to track the location (good to conserve battery power):
1 |
[self.manager stopUpdatingLocation]; |
Reacting to updates
Now that the manager is running we can implement two methods that will be called by the manager every time there is an update or a failure to get a location. For this to work we need to conform to the following protocol, and of course import the CoreLocation framework (don’t forget to link the latter in your project):
1 2 |
@import CoreLocation; @interface ViewController : UIViewController <CLLocationManagerDelegate> |
Here are the delegate methods we’re interested in:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { // grab current location and display it in a label CLLocation *currentLocation = [locations lastObject]; NSString *longText = [NSString stringWithFormat:@"%f", currentLocation.coordinate.longitude]; NSString *latText = [NSString stringWithFormat:@"%f", currentLocation.coordinate.latitude]; NSString *accuracy = [NSString stringWithFormat:@"%f", currentLocation.horizontalAccuracy]; self.longLabel.text = longText; self.latLabel.text = latText; self.accuracyLabel.text = accuracy; } - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { // didn't get a location } |
[/emember_protected]
In the first method all I’m doing is to display the longitude and latitude in two labels. Both are float values, derived from a CLLocation object that is available in the locations array (this is new in iOS 7). This array contains previous location objects as well and is amended every time this method is called. The most current location is at the end of the array (we get it via [locations lastObject]).
I’ve created a Demo Project on GitHub called Locations – feel free to fork and examine how it works.
- https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html#//apple_ref/occ/cl/CLLocationManager
- https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocation_Class/CLLocation/CLLocation.html
- https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009497