我想通过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
通过这次我只得到了高度值,但我需要知道如何计算平均海拔最低海拔和最高海拔。有谁知道如何做到这一点?
只需存储您在数组中获得的所有高度,然后在您需要查找min,max,avg时遍历它们? – vakio 2011-05-31 09:38:04
锄头发现,你能否详细解释我,谢谢 – Rocky 2011-05-31 10:39:00