我想在几分钟后立刻获得当前位置与新(当前)位置之间的距离。我想要一个计时器,每隔一秒就用新距离更新我的标签。如何获得我的当前位置和新位置之间的距离?
示例:我站在街道上并与当前位置相距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];
}
有什么问题与您的代码?看起来不错。但它会始终显示您的最后两个位置之间的距离,时间差为1秒。你没有保持你的第一个位置。我不确定你想要这个。 – ismailgulek