I want to get the distance between my current position and a new (current) position a few moments later. I want a timer that updates my label every second with the new distance.
Example: am standing on a street with my current location and run 300 meters further. I get a new current location and a label should give 300 meters.
I am not very experienced in objected c-programming. this is what i got so far:
#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];
}
The code wont work because locationManager.location
cant be called that way.
I have added speed to my app. It works but only wenn the manager sends new information.
@implementation ViewController
{
CLLocationManager *locationManager;
double distance;
double speed;
double extraDistance;
NSMutableArray *locations;
CLLocation *newLocation2;
CLLocation *oldLocation2;
CLLocation *newLocationCheck;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
distance =0;
speed =0;
locationManager = [[CLLocationManager alloc] init];
locations = [[NSMutableArray alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self startTimer];
[locationManager startUpdatingLocation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)startTimer
{
// Timer that repeatedly does something every 2 seconds
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(updateSpeedDistance:) userInfo:nil repeats:YES];
}
- (void)updateSpeedDistance:(NSTimer *)timer
{
// fetches the old and new location out of the array list
oldLocation2 = locations[0];
newLocation2 = locations[1];
// checks if theres a new location
if (newLocation2 != newLocationCheck) {
// if there is only one following location
if (oldLocation2 != newLocation2){
distance = distance + [newLocationCheck distanceFromLocation:newLocation2];
speed = [oldLocation2 distanceFromLocation: newLocation2] / 2;
newLocationCheck = newLocation2;
// if there multiple locations avaible
}
else {
distance = distance + [oldLocation2 distanceFromLocation:newLocation2];
speed = [oldLocation2 distanceFromLocation: newLocation2] / 2;
newLocationCheck = newLocation2;
}
}
// if there's no new location the speed wil drop
else {
speed = speed /1.2;
}
self.lbDistance.text = [NSString stringWithFormat:@"Distance: %1.2f m", distance];
self.lbSpeed.text = [NSString stringWithFormat:@"Speed: %1.2f m/s", speed];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
if (newLocation != nil && oldLocation != nil) {
[locations removeAllObjects];
[locations addObject:oldLocation];
[locations addObject:newLocation];
}
else {
[locationManager startUpdatingLocation];
}
}
@end