Codesnipp.it Social Code Sharing

Amber Weinberg

Playing With Core Location on iPhone

by Amber Weinberg on Oct 28, 2010

/* From Beginning iPhone Development 3 by Dave Mark */ /* This app will display your current distance, altitude, level of accuracy and distance traveled if moving. You'll need to import the CoreLocation framework*/ /*In your viewController.h file*/ #import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> @interface _4_WhereAmIViewController : UIViewController <CLLocationManagerDelegate> { CLLocationManager *locationManager; CLLocation *startingPoint; UILabel *latitudeLabel; UILabel *longitudeLabel; UILabel *horizontalAccuracyLabel; UILabel *altitudeLabel; UILabel *verticalAccuracyLabel; UILabel *distanceTraveledLabel; } @property (nonatomic, retain) CLLocationManager *locationManager; @property (nonatomic, retain) CLLocation *startingPoint; @property (nonatomic, retain) IBOutlet UILabel *latitudeLabel; @property (nonatomic, retain) IBOutlet UILabel *longitudeLabel; @property (nonatomic, retain) IBOutlet UILabel *horizontalAccuracyLabel; @property (nonatomic, retain) IBOutlet UILabel *altitudeLabel; @property (nonatomic, retain) IBOutlet UILabel *verticalAccuracyLabel; @property (nonatomic, retain) IBOutlet UILabel *distanceTraveledLabel; @end /*in your viewController.m file*/ #import "_4_WhereAmIViewController.h" @implementation _4_WhereAmIViewController @synthesize locationManager; @synthesize startingPoint; @synthesize latitudeLabel; @synthesize longitudeLabel; @synthesize horizontalAccuracyLabel; @synthesize altitudeLabel; @synthesize verticalAccuracyLabel; @synthesize distanceTraveledLabel; #pragma mark - - (void)viewDidLoad { self.locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyBest; [locationManager startUpdatingLocation]; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } - (void)viewDidUnload { self.locationManager = nil; self.latitudeLabel = nil; self.longitudeLabel = nil; self.horizontalAccuracyLabel = nil; self.altitudeLabel = nil; self.verticalAccuracyLabel = nil; self.distanceTraveledLabel = nil; [super viewDidUnload]; } - (void)dealloc { [locationManager release]; [startingPoint release]; [latitudeLabel release]; [longitudeLabel release]; [horizontalAccuracyLabel release]; [altitudeLabel release]; [verticalAccuracyLabel release]; [distanceTraveledLabel release]; [super dealloc]; } #pragma mark - #pragma mark CLLocationManagerDelegate Methods - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { if (startingPoint == nil) self.startingPoint = newLocation; NSString *latitudeString = [[NSString alloc] initWithFormat:@"%g°", newLocation.coordinate.latitude]; latitudeLabel.text = latitudeString; [latitudeString release]; NSString *longtitudeString = [[NSString alloc] initWithFormat:@"%g°", newLocation.coordinate.longitude]; longitudeLabel.text = longtitudeString; [longtitudeString release]; NSString *horizontalAccuracyString = [[NSString alloc] initWithFormat:@"%gm", newLocation.horizontalAccuracy]; horizontalAccuracyLabel.text = horizontalAccuracyString; [horizontalAccuracyString release]; NSString *altitudeString = [[NSString alloc] initWithFormat:@"%gm", newLocation.altitude]; altitudeLabel.text = altitudeString; [altitudeString release]; NSString *verticalAccuracyString = [[NSString alloc] initWithFormat:@"%gm", newLocation.verticalAccuracy]; verticalAccuracyLabel.text = verticalAccuracyString; [verticalAccuracyString release]; CLLocationDistance distance = [newLocation getDistanceFrom:startingPoint]; NSString *distanceString = [[NSString alloc] initWithFormat:@"%gm", distance]; distanceTraveledLabel.text = distanceString; [distanceString release]; } - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSString *errorType = (error.code == kCLErrorDenied) ? @"Access Denied" : @"Unknown error"; UIAlertView *alert = [[UIAlertView alloc ] initWithTitle:@"Error getting location" message:errorType delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil]; [alert show]; [alert release]; } @end

Can't see the comments? Please login first :)