2

を使用して、現在の場所の最大高度、最小高度、および平均高度を計算したいと思いますCLLocationManager。次のコードを使用して高度を計算する方法を知っています。

    #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

これにより、高度値のみを取得できますが、平均高度、最小高度、および最大高度の計算方法を知る必要があります。誰かがこれを行う方法を知っていますか?

4

2 に答える 2

3

他の人が示唆しているようにすべての高度を配列に保存する代わりに、現在の平均/分/最大を保存して、必要に応じて更新することができます。

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;
}
于 2011-06-01T13:44:07.893 に答える
2

メソッドでminを取得する方法について説明しますminAltitude。最大値と平均値を見つけるのはあなたに任せます。

.hで:

NSMutableArray *altitudes;

.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;
}
于 2011-05-31T11:59:33.063 に答える