The Core Location framework provides the interfaces for obtaining information about the user’s location. With the GPS coordinate obtained, you can make use of the google API to decode the actual street or utilize the Map framework to further display the location on Map. Read Apple Documentation

Steps:

  1. Create a new Xcode Project for (Single View Application).
  2. Give Project a name ( Lets say CurrentLocationDemo).
  3. Click next button and the Project will be created.
  4. Now go to Main StoryBoard of the Project and In the Object Library, select the “Label” object and drag it into the view. Take two Labels.
  5. Create  IBoutlet for the “Label” in the ViewContoller.h.
  6.  Next we have to add core Location framework in our project. In the Project Navigator, select the “CurrentLocationDemo” project. In the Content Area, select “CurrentLocationDemo” under Targets and click “Build Phases”. Expand “Link Binary with Libraries” and click the “+” button to add the CoreLocation framework.
  7. Now import  <CoreLocation/CoreLocation.h> in your ViewController.h.
  8. Core Location makes use of the delegate pattern. To use Core Location framework, we must set this protocol (CLLocationManagerDelegate protocol).

 

 

After Connectimg the IBOutlet and writing the delegates Your View Controller will Look something Like this

//

// ViewController.h

// CurrentLocationDemo

//

// Created by Nilesh on 4/14/16.

// Copyright © 2016 Nilesh. All rights reserved.

//

#import <UIKit/UIKit.h>

#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController<CLLocationManagerDelegate>

@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;

@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;

 

@end

9. Now go to Main StoryBoard of the Project and In the Object Library, select the “Button” object and drag it into the view.

10. Make connection for Button Action in “ViewController.m” and then make the object of  CLLocationManagerbelow the interface section,  write the below code in that.

 

@implementation ViewController{

CLLocationManager *locationManager;

}

 

– (void)viewDidLoad {

[super viewDidLoad];

locationManager = [[CLLocationManager alloc] init];

// Do any additional setup after loading the view, typically from a nib.

}

#pragma mark – Action Methods

– (IBAction)getCurrentLocation:(id)sender {

locationManager = [[CLLocationManager alloc] init];

locationManager.delegate = self;

// Check for iOS 8. Without this guard the code will crash with “unknown selector” on iOS 7.

if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {

[locationManager requestWhenInUseAuthorization];

}

[locationManager startUpdatingLocation];

}

11. Next You have to implement delegate methods of CLLocationManager.

#pragma mark – Location Delegate Methods

– (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

{

NSLog(@”********FailedWithError********: %@”, error);

UIAlertView *errorAlertView = [[UIAlertView alloc]

initWithTitle:@”Error” message:@”Failed to Get Your Location. Please Try again Later” delegate:nil cancelButtonTitle:@”OK” otherButtonTitles:nil];

[errorAlertView show];

}

– (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

{

NSLog(@”********UpdateToLocation********: %@”, newLocation);

CLLocation *currentLocation = newLocation;

if (currentLocation != nil) {

_longitudeLabel.text = [NSString stringWithFormat:@”%.8f”, currentLocation.coordinate.longitude];

_latitudeLabel.text = [NSString stringWithFormat:@”%.8f”, currentLocation.coordinate.latitude];

}

}

@end

12. If You are running this code in the Simulator then You will have to change the Simulator Location to Apple, By default its none.  Now run the app and see the desired output. Enjoy Coding 🙂

Advertisements