Reputation: 1738
What is wrong here? I am trying to get total distance travelled but when I start driving back to the point I started my value goes down? What needs to be fixed. Thank you.
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if (startLocation == nil)
self.startLocation = newLocation; //if this is the first update colocation called startlocation = new location
CLLocationDistance distanceBetween = [newLocation
distanceFromLocation:startLocation]; //here is my problem I think
NSString *tripString = [[NSString alloc] //convert to string
initWithFormat:@"%f",
distanceBetween];
distance.text = tripString; //update my distance label called distance
}
Upvotes: 0
Views: 126
Reputation: 24229
try this approach:
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if (startLocation == nil)
{
self.totalDistanceBetween = 0; // declare this variable
self.startLocation = newLocation;
}
CLLocationDistance distanceBetween = [newLocation distanceFromLocation:startLocation ];
startLocation = newLocation;
self.totalDistanceBetween += distanceBetween; // declare this variable
NSString *tripString = [[NSString alloc] //convert to string
initWithFormat:@"%f",
self.totalDistanceBetween];
distance.text = tripString; //update my distance label called distance
}
Upvotes: 1