YouTip LogoYouTip

Ios Location

iOS Location Operations


Introduction

In iOS, location services are implemented via CoreLocation, enabling retrieval of the user’s current position as well as device movement information.

Step-by-step Example

  1. Create a simple View-based Application.
  2. Select your project file, then select the target, and add CoreLocation.framework, as shown below:

Image 1: CoreLocation_Library_Addition

  1. In ViewController.xib, add two labels and create IBOutlets named latitudeLabel and longitudeLabel.
  2. Now select File β†’ New β†’ File... β†’, choose Objective-C class, and click Next.
  3. Set β€œSubclass of” to NSObject, and name the class LocationHandler.
  4. Click Create.
  5. 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
  1. 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];
        ;
    }
    return DefaultManager;
}

-(void)initiate
{
    locationManager = [init];
    locationManager.delegate = self;
}

-(void)startUpdating
{
    ;
}

-(void) stopUpdating
{
    ;
}

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation: (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    if ([self.delegate respondsToSelector:@selector (didUpdateToLocation:fromLocation:)]) 
    {
        [self.delegate didUpdateToLocation:oldLocation fromLocation:newLocation];
    }
}
@end
  1. Update ViewController.h as follows:
#import <UIKit/UIKit.h>
#import "LocationHandler.h"

@interface ViewController : UIViewController<LocationHandlerDelegate>
{
    IBOutlet UILabel *latitudeLabel;
    IBOutlet UILabel *longitudeLabel;
}
@end
  1. Update ViewController.m as follows:
#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

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

- (void)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 this application, the following output appears:

Image 2: locationOutput

← Ios SqliteIos Camera β†’