Wednesday, June 27, 2012

How to develop an IPhone Location App


You can say the continuation to my prev article on how to use the HTML5 GeoLocation api, but its different this time. It uses Objective C to get the location details rather than using HTML5.

Is building IOS App easy ?

I asked my self this question many times with out knowing how easy it is  before I attended the work shop conducted by Dr.Barbara Hecker.

My 4th App (other than my 3 hello world app) I created this Location Detection app which detects your position and displays latitude & longitude on the iPhone screen.

All you need is  Xcode (I used Xcode Version 4.3.3), IOS 5 SDK, iPhone Simulator and little bit of scripting knowledge :)

For who know how to create an app this should be simple.

Create new project using a Single View Application. This will create a default ViewController Interface & Implementation class (ViewController.h and ViewController.m). Download the code from here

To implement the location interface all you need to know about is LocationManager in IOS. This CLLocationManage is the interface to deliver the location events. The main objective is to deliver event, create instance and attach a delegate (CLLocationManageDelegate) object to it.
For more info on LocationManager refer this link.

In this I used the below properties
1) delegate property for configuring the updates
2) startUpdatingLocation to start the updates 

Make sure you import CoreLocation library (Target > Build Phases > Add CoreLocation under Link binary with libraries. It took me quite a while to figure it out properly. If you get "undefined symbols for architecture i386 _OBJC_CLASS_$_CLLocationManager" don't be panic)

Cut to chase, below is the sample implementation details.

//  ViewController.h
#import
#import
@interface ViewController : UIViewController

<CLLocationManagerDelegate
{
    IBOutlet UITextField *latitudeText;
    IBOutlet UITextField *longitudeText;
    CLLocationManager *locMgr;
}

@property (retain,nonatomic) IBOutlet UITextField *latitudeText;
@property (retain,nonatomic) IBOutlet UITextField *longitudeText;

@property (weak, nonatomic) IBOutlet UIButton *locationDetect;

@end

Using the story board I added 2 text fields (for latitude & longitude) and control+click on the textFields and drag it to interface class. This will create property for each text fields like below

@property (retain,nonatomicIBOutlet UITextField *latitudeText;

Also I created button, which on click will get the location.

Below is my implementation class

//  ViewController.m
#import "ViewController.h"
@implementation ViewController

@synthesize latitudeText,longitudeText,locationDetect; 
CLLocationManager *locMgr;

......
- (void)viewDidLoad
{
    [super viewDidLoad];
    locMgr = [[CLLocationManager alloc]init];
    [locMgr startUpdatingLocation];
}

......

- (IBAction)locationDetect:(id)sender {
    NSString *latitudeTextData = [[NSString
                                   alloc]initWithFormat:@"%g",locMgr.location.coordinate.latitude];
    NSString *longitudeTextData =[[NSString alloc]
                                  initWithFormat:@"%g",locMgr.location.coordinate.longitude];
    self.latitudeText.text = latitudeTextData;
    self.longitudeText.text = longitudeTextData;
}

@end

In this I created a LocationManager object and started the location update process by using the startUpdatingLocation method.  Then capture the values from the location manager and update the text field.

Isn't quite simple ;)







2 comments:

Unknown said...

choose CoreLocation.frameWork instead of CoreGraphics.frameWork after to the appropriate Frameworks folder.

Unknown said...
This comment has been removed by the author.