Wednesday, March 24, 2010

Location tracking in IPhone

IPhone provides you the ability to get your location and relies on cell tower triangulation as well as a built in GPS.
In this tutorial I will demonstrate you how to find you Location using a small IPhone App.
You can Integrate a MKMapKit/MKMapView to enhance this to make the output more graphical.
I will try to provide a small demo on that if I get time later.

Add the Core Location framework to your project to use the location data.
Add a few UILabel controls for displaying the longitude and latitude.For updating the labels we will need the associated
IBOutlets to these labels.If you are not familiar with IBAction/Outlets then refer to the apple documentation.
The sample code is as below


#import "<UIKit/UIKit.h>"

@interface MyLocationViewController : UIViewController {
  IBOutlet UILabel *latitude;
  IBOutlet UILabel *longitude;
}

@end


We will use the CLLocationManager class to send updates to a delegate when our the location changes.
The delegate associated to this class is CLLocationManagerDelegate so our view controller should implement
this delegate.

#import "<UIKit/UIKit.h>"
#import "<CoreLocation/CoreLocation.h>"

@interface MyLocationViewController : UIViewController < CLLocationManagerDelegate >
  {
  CLLocationManager *locationManager;
  IBOutlet UILabel *latitude;
  IBOutlet UILabel *longitude;
}

@end

In the viewDidLoad of the Viewcontroller configure the CLLocationManager settings like distanceFilter,desiredAccuracy etc and then call the startUpdatingLocation function of the CLLocationManager to get the delegate working.

- (void)viewDidLoad {
  [super viewDidLoad];
  locationManager = [[CLLocationManager alloc] init];
  locationManager.delegate = self;
  locationManager.distanceFilter = kCLDistanceFilterNone;
  locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
  [locationManager startUpdatingLocation];
}

From the  delegate protocol implement the following delegate in your viewController

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


The full body of the implementation will be

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
  int degrees = newLocation.coordinate.latitude;
  double decimal = fabs(newLocation.coordinate.latitude - degrees);
  int minutes = decimal * 60;
  double seconds = decimal * 3600 - minutes * 60;
  NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
                   degrees, minutes, seconds];
  latitude.text = lat;
  degrees = newLocation.coordinate.longitude;
  decimal = fabs(newLocation.coordinate.longitude - degrees);
  minutes = decimal * 60;
  seconds = decimal * 3600 - minutes * 60;
  NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
                     degrees, minutes, seconds];
  longitude.text = longt;
}
Here we take the location and turn the degrees into degrees, minutes, and seconds then display the value in the correct label.
For the mathematical calculation remember Each 1 degree= 60 minutes = 3600 seconds. 

Getting a hang of delegate feature in IPhone is the core to this end of programming so master that first and then get into some serious IPhone programming.