问题描述
||
我想通过
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
通过此操作,我仅获得高度值,但是我需要知道如何计算平均高度,最小高度和最大高度。有谁知道如何做到这一点?
解决方法
我描述了如何在
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;
}
, 不必像其他人建议的那样将所有海拔高度存储在数组中,而是可以存储当前的平均值/最小值/最大值并随时进行更新。
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;
}