Skip to content

Instantly share code, notes, and snippets.

@twoism
Created October 29, 2009 15:11
Show Gist options
  • Save twoism/221501 to your computer and use it in GitHub Desktop.
Save twoism/221501 to your computer and use it in GitHub Desktop.
#import "CLLocation+Vector.h"
#define CARDINAL_DIRECTIONS [NSArray arrayWithObjects:@"North",@"Northeast",@"East",@"Southeast",@"South",@"Southwest",@"West",@"Northwest",nil]
@implementation CLLocation(Vectors)
- (float)angleBetween:(CLLocationCoordinate2D)coord
{
float lat1 = self.coordinate.latitude * M_PI / 180.0;
float lat2 = coord.latitude * M_PI / 180.0;
float lon1 = self.coordinate.longitude * M_PI / 180.0;
float lon2 = coord.longitude * M_PI / 180.0;
float b = atan2f(sinf(lon2-lon1)*cosf(lat2), cosf(lat1)*sinf(lat2)-sinf(lat1)*cosf(lat2)*cosf(lon2-lon1));
float a = (2*M_PI);
float c = b - (floor(b/a) * a);
return (c * (180/ M_PI));
}
- (NSString*)directionFromCoordinate:(CLLocationCoordinate2D)coord
{
float degrees = [self angleBetween:coord];
if (degrees > 0) {
return [CARDINAL_DIRECTIONS objectAtIndex:((int)floor((fabs(degrees) + 22.5)/45) )];
}else{
return @"";
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment