iphone - 如何通过iPhone中的GPS位置管理器计算平均海拔

标签 iphone objective-c locationmanager

我想通过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

通过这个我只得到海拔值,但我需要知道如何计算平均海拔、最小海拔和最大海拔。有谁知道该怎么做吗?

最佳答案

您可以只存储当前的平均值/最小值/最大值并随时更新它,而不是像其他人建议的那样将所有海拔高度存储在数组中。

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;
}

关于iphone - 如何通过iPhone中的GPS位置管理器计算平均海拔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6185612/

相关文章:

iphone - 如何在 'for' 循环中添加 subview

ios - 如何从 iOS SDK 获取特定的 iPhone 信息

ios - startRangingBeaconsSatisfyingConstrain多个信标

iphone - 添加项目时 UITableView 调整大小

ios - 尝试使用 UIPickerView 选择要在 UIImageView 中显示的图像

iphone - iOS 3.1.3 上的手势检测

iOS WKWebview 如何检测何时单击 <a> 标签内的图像

iphone - 为什么 viewDidAppear 没有被触发?

android - requestLocationUpdates 不会在 Android 中按时间间隔更新

java - NETWORK_PROVIDER 无法在 Android 中工作