几分钟后,我想获得当前位置与新(当前)位置之间的距离。我想要一个计时器,每秒用新的距离更新我的标签。
示例:我站在我当前位置的街道上,然后再跑 300 米。我得到一个新的当前位置,标签应该给出 300 米。
我在反对 c 编程方面不是很有经验。这是我到目前为止得到的:
#import <CoreLocation/CoreLocation.h>
@interface NormalSoloViewController () <CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *lbDistance;
@end
@implementation NormalSoloViewController
{
CLLocationManager *locationManager;
CLLocation *firstLocation;
}
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager =[[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLHeadingFilterNone;
[locationManager startUpdatingLocation];
firstLocation = locationManager.location;
}
- (void)startTimer
{
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateDistance:) userInfo:nil repeats:YES];
}
- (void)updateDistance:(NSTimer *)timer
{
double distance = 0;
distance = distance + [self calculateDistance];
self.lbDistance.text = [NSString stringWithFormat:@"%f", distance];
}
- (double) calculateDistance {
CLLocation *newFirstLocation = firstLocation;
CLLocation *secondLocation = locationManager.location;
firstLocation = secondLocation;
return [newFirstLocation distanceFromLocation:secondLocation];
}