Rocky
Rocky

Reputation: 1423

How to calculate the average altitude through gps location manager in iphone

I want to calculate the maximum altitude, minimum altitude, and average altitude of the current location through CLLocationManager. I know how to calculate the altitude using the following code:

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

    @interface test : UIViewController <CLLocationManagerDelegate> {
        CLLocationManager   *locationManager;

        CLLocation  *startingPoint;

        IBOutlet    UILabel *altitudeLabel;

    }
    @property (retain, nonatomic) CLLocationManager *locationManager;
    @property (retain, nonatomic) CLLocation *startingPoint;
    @property (retain, nonatomic) UILabel *altitudeLabel;
    @end
    //this is my test.h class




    #import "test.h"

    @implementation test
    @synthesize locationManager;
    @synthesize startingPoint;
    @synthesize altitudeLabel;


    #pragma mark -
    - (void)viewDidLoad {
        self.locationManager = [[CLLocationManager alloc] init];
        [locationManager startUpdatingLocation];
        locationManager.delegate = self;
        locationManager.distanceFilter = kCLDistanceFilterNone; 
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    }
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }

    - (void)dealloc {

        [locationManager release];
        [startingPoint release];
        [altitudeLabel 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 *altitudeString = [[NSString alloc] initWithFormat:@"%gm", newLocation.altitude];
        altitudeLabel.text = altitudeString;
        [altitudeString release];



    }

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

        NSString *errorType = (error.code == kCLErrorDenied) ? @"Access Denied" : @"Unknown Error";
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error gettingg location from Core Location" message:errorType delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
        [alert show];
        [alert release];

    }

    @end

Through this I only get the altitude value, but I need to know how to calculate average altitude, minimum altitude, and maximum altitude. Does anyone know how to do this?

Upvotes: 2

Views: 1852

Answers (2)

brain
brain

Reputation: 5546

Instead of storing all the altitudes in an array as others have suggested, you could just store the current average/min/max and update it as you go.

int numUpdates = 0;
double averageAlt = 0.0;
double minAlt = DBL_MAX;
double maxAlt = DBL_MIN;

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
      if (newLocation.altitude < minAlt) {
          minAlt = newLocation.altitude;
      }
      if (newLocation.altitude > maxAlt) {
          maxAlt= newLocation.altitude;
      }
      double sum = numUpdates * averageAlt;
      sum+=newLocation.altitude;
      numUpdates++;
      averageAlt = sum / numUpdates;
}

Upvotes: 3

vakio
vakio

Reputation: 3177

I describe how to get min in the minAltitude method. I'll leave it to you to find max and average.

in .h:

NSMutableArray *altitudes;

in .m:

- (void) viewDidLoad {
    [super viewDidLoad];
    altitudes = [[NSMutableArray alloc] init];
}

- (void) dealloc {
    [altitudes release];
    [super dealloc];
}

 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
      [altitudes addObject:[NSNumber numberWithDouble:newLocation.altitude]];
}

- (double) minAltitude 
{
     double min = DBL_MAX;
     double value;
     NSNumber *altitude;
     for (altitude in altitudes) {
         value = [altitude doubleValue];
         if (value < min) {
             min = value;
         }
     }

     return min;
}

Upvotes: 2

Related Questions