How To Measure Altitude In iPhone

In this application we will see how to measure Altitude in different places using GPS access.

Step 1: Create a View base application using template . Give the application name “HeightMeasurement”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: We need to add CoreLocation.framework in our framework folder, so select Frameworks -> add -> Existing frameworks -> Frameworks -> and select CoreLocation.

Step 4: Open the HeightMeasurementViewController.h file, we have added UILabel for display the Altitude. CLLocationManager class defines the interface for configuring the delivery of location and heading-related events to your application , and add two method. So make the following changes in the file.


@interface HeightMeasurementViewController : UIViewController {
IBOutlet UILabel *heightMesurement;
CLLocationManager *locmanager;

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

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *) error;



Step 5: Double click your HeightMeasurementViewController.xib file and open it to the Interface Builder. Drag Label from the library and place it to the view window, select the label and bring up Attributes Inspector, change the font size and style . Now connect the File’s Owner icon to the View icon and select view. Connect File’s Owner icon to the label and select heightMeasurement. Now save the HeightMeasurementViewController.xib file and close it and go back to the Xcode.

Step 6: In the HeightMeasurementViewController.m file make the following changes:


-(void)awakeFromNib {
locmanager = [[CLLocationManager alloc] init];
[locmanager setDelegate:self];
[locmanager setDesiredAccuracy:kCLLocationAccuracyBest];
[locmanager startUpdatingLocation];


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
heightMesurement.text = [NSString stringWithFormat: @"%.2f m", newLocation.altitude];

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
heightMesurement.text = @"0.00 m";



Step 7: Now compile and run the application in the simulator, if you want to see actual output then you have to see in the iPhone device.

You can downloaded SourceCode from here HeightMeasurement 2

Leave a Comment:

1 comment
Patrick says March 24, 2010

I had to make the following two changes to your code, are my changes necessary?


@interface HeightMeasurementViewController: UIViewController

Add Your Reply