2011-05-31 45 views
2

我想通過CLLocationManager來計算當前位置的最大高度,最小高度和平均海拔高度。我知道如何使用下面的代碼來計算高度:如何通過iphone中的GPS定位管理器計算平均海拔

#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 

通過這次我只得到了高度值,但我需要知道如何計算平均海拔最低海拔和最高海拔。有誰知道如何做到這一點?

+0

只需存儲您在數組中獲得的所有高度,然後在您需要查找min,max,avg時遍歷它們? – vakio 2011-05-31 09:38:04

+0

鋤頭髮現,你能否詳細解釋我,謝謝 – Rocky 2011-05-31 10:39:00

回答

2

我描述瞭如何在minAltitude方法中獲得min。我會留給你找到最大和平均。

在.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; 
} 
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; 
} 
+0

或者只存儲海拔的總數和數量,並在需要的時候找到平均值。 – 2011-06-01 13:50:16

+0

@Alexsander - 好點,我想象它在GUI上顯示並且一直在更新。 – brain 2011-06-01 14:28:19