Skip to content

Instantly share code, notes, and snippets.

@patelrohan
Last active December 10, 2015 19:38
Show Gist options
  • Save patelrohan/4482451 to your computer and use it in GitHub Desktop.
Save patelrohan/4482451 to your computer and use it in GitHub Desktop.
Distance calculation using Locationamanger
-(void)findCurrentLocation
{
if(nil==self.locationManager)
{
self.locationManager= [[[CLLocationManager alloc]init]autorelease];
}
locationManager.delegate=self;
//locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
//locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.distanceFilter = 2.0;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
currentLocation=[locations lastObject];
NSDate* eventDate = currentLocation.timestamp;
NSLog(@"Current time stamp %@",eventDate);
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
NSLog(@"How recent %d",abs(howRecent));
if (abs(howRecent) < 15.0)
//if (howRecent < -0.0 && howRecent > -10.0)
{
if([locationArray count]>1)
{
previousLocation=[locationArray objectAtIndex:[locationArray count]-1];
long double dist=[currentLocation distanceFromLocation:previousLocation];
if(dist < 5.0)
{
totalDistance= totalDistance + dist;
[viewControllerDelegate updateDistanceLabel];
}
}
//NSLog(@"from singleton class lat---> %f - long---->%f", currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);
}
[locationArray addObject:[locations lastObject]];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment