Easy Tutorial
❮ Ios Action Outlet Att Ios Ui Scrollview ❯

IOS Location Operation


Introduction

In iOS, using CoreLocation for positioning, you can obtain the user's current location and also get the device's movement information.

Example Steps

  1. Create a simple View-based application.

  2. Select the project file, then select the target, and add CoreLocation.framework as shown below.

  3. Add two labels in ViewController.xib, create ibOutlet labels named latitudeLabel and longitudeLabel.

  4. Now, go to "File -> New -> File... ->" and select Objective-C class and click Next.

  5. Set "subclass of" to NSObject and name the class LocationHandler.

  6. Choose Create.

  7. Update LocationHandler.h as follows:

    #import <Foundation/Foundation.h>
    #import <CoreLocation/CoreLocation.h>
    
    @protocol LocationHandlerDelegate <NSObject>
    
    @required
    -(void) didUpdateToLocation:(CLLocation*)newLocation 
     fromLocation:(CLLocation*)oldLocation;
    @end
    
    @interface LocationHandler : NSObject<CLLocationManagerDelegate>
    {
        CLLocationManager *locationManager;
    }
    @property(nonatomic,strong) id<LocationHandlerDelegate> delegate;
    
    +(id)getSharedInstance;
    -(void)startUpdating;
    -(void) stopUpdating;
    
    @end
    
  8. Update LocationHandler.m as follows:

    #import "LocationHandler.h"
    static LocationHandler *DefaultManager = nil;
    
    @interface LocationHandler()
    
    -(void)initiate;
    
    @end
    
    @implementation LocationHandler
    
    +(id)getSharedInstance{
        if (!DefaultManager) {
            DefaultManager = [[self allocWithZone:NULL]init];
            [DefaultManager initiate];
        }
        return DefaultManager;
    }
    -(void)initiate{
        locationManager = [[CLLocationManager alloc]init];
        locationManager.delegate = self;
    }
    
    -(void)startUpdating{
        [locationManager startUpdatingLocation];
    }
    
    -(void) stopUpdating{
        [locationManager stopUpdatingLocation];
    }
    -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:
     (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
        if ([self.delegate respondsToSelector:@selector
        (didUpdateToLocation:fromLocation:)]) 
        {
            [self.delegate didUpdateToLocation:oldLocation 
            fromLocation:newLocation];
    
        }
    }
    
    @end
    
  9. Update ViewController.h as follows:

    #import <UIKit/UIKit.h>
    #import "LocationHandler.h"
    @interface ViewController : UIViewController<LocationHandlerDelegate>
    {
        IBOutlet UILabel *latitudeLabel;
        IBOutlet UILabel *longitudeLabel;
    }
    @end
    
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[LocationHandler getSharedInstance]setDelegate:self];
    [[LocationHandler getSharedInstance]startUpdating];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)didUpdateToLocation:(CLLocation *)newLocation 
 fromLocation:(CLLocation *)oldLocation{
    [latitudeLabel setText:[NSString stringWithFormat:
    @"Latitude: %f",newLocation.coordinate.latitude]];
    [longitudeLabel setText:[NSString stringWithFormat:
    @"Longitude: %f",newLocation.coordinate.longitude]];

}

@end

Output

When we run the application, we get the following output:

❮ Ios Action Outlet Att Ios Ui Scrollview ❯