比较速度值问题(我从CLLocationManager得到什么)

对于这种提问方式,我有点困惑

任何我如何提到我的查询支持图像在这里。 你能帮我解决这个问题吗?

第1步:我有一个要求:通过使用CLLocationManger委托方法我提取速度值,如:

- (void)startLocationUpdates{ locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.distanceFilter = kCLDistanceFilterNone; locationManager.desiredAccuracy = kCLLocationAccuracyBest; locationManager.pausesLocationUpdatesAutomatically = YES; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) [locationManager requestWhenInUseAuthorization]; [locationManager startUpdatingLocation]; } #pragma mark #pragma mark - CLLocationManagerDelegate - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ float speed = locationManager.location.speed; float speedInKmph = speed * 3.6; // to convert the speed into kmph. NSString *speedValue =[NSString stringWithFormat:@"%.f Kmph ",speedInKmph]; self.currentSpeedLblRef.text = speedValue; self.maxSpeedLblRef.text = speedValue; } 

第2步: currentSpeedLblRef,maxSpeedLblRef – >这些是我的UILabels

示例:现在我正在开车 – >第一次打开应用程序,我得到了当前的车速(如: 120 Kmph ),然后我需要在“maxSpeedLblRef”( 120 Kmph)中显示相同的值

一段时间后,我目前的车速如果50公里每小时。 但我需要显示值在“maxSpeedLblRef”是 – >最大值 – >是指120公里每小时。 因为我已经得到了前面120公里的价值

之后,如果我目前的车速如果180公里每小时 – >我需要显示“maxSpeedLblRef”的价值,如:180 Kmph。 因为它是最新的一个120公里/小时

closures应用程序后,然后

如果我重新打开应用程序,我想要显示的值,如“maxSpeedLblRef” – > 180 Kmph。 因为这个值是以前保存的值

在这里输入图像说明

这是我的源代码 : 点击这里链接

你在这里错过了一些简单的事情!

您需要存储最大速度进行比较 – 您所做的只是每次更新当前值的标签。 设置一个类级属性,初始值= 0,并在当前速度>最大速度时更新它。

有几种方法可以存储最大值,以便在下次打开应用程序时存在。 可能最容易去用户默认。 有很多教程可用 – 试试这个http://code.tutsplus.com/tutorials/ios-sdk-working-with-nsuserdefaults–mobile-6039

好的 – 使用你的项目代码,这里是你需要的。

更新你的ViewController.h到这个

 // ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UILabel *currentSpeedLblRef; @property (weak, nonatomic) IBOutlet UILabel *maxSpeedLblRef; // you need to store the max speed @property float speedMax; -(void)loadDefaults; -(void)storeDefaults; @end 

然后在VIewController.m中,用这个replaceviewDidLoad

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self loadDefaults]; [self startLocationUpdates]; } 

更新locationManager函数

 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ float speed = locationManager.location.speed; float speedInKmph = speed * 3.6; // to convert the speed into kmph. NSString *speedValue =[NSString stringWithFormat:@"%.f Kmph ",speedInKmph]; self.currentSpeedLblRef.text = speedValue; if (speedInKmph > self.speedMax) { self.speedMax = speedInKmph; [self storeDefaults]; } NSString *speedMaxValue =[NSString stringWithFormat:@"%.f Kmph ",self.speedMax]; self.maxSpeedLblRef.text = speedMaxValue; } 

最后添加加载/存储function

 - (void)loadDefaults { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; self.speedMax = [defaults floatForKey:@"SpeedMax"]; } - (void)storeDefaults { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setFloat:self.speedMax forKey:@"SpeedMax"]; [defaults synchronize]; }