Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated for modern objective-c #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions Haversine.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,13 @@ extern float const HAVERSINE_KM_RADIUS;
extern float const HAVERSINE_M_PER_KM;
extern float const HAVERSINE_F_PER_MI;

@interface Haversine : NSObject {
float lat1;
float lon1;
float lat2;
float lon2;
}
@interface Haversine : NSObject

@property CLLocationCoordinate2D point1;
@property CLLocationCoordinate2D point2;

@property float lat1;
@property float lon1;
@property float lat2;
@property float lon2;
- (id)initWithPoint1:(CLLocationCoordinate2D)point1 andPoint2:(CLLocationCoordinate2D)point2 ;

- (id)init;
- (id)initWithLat1:(float)newLat1
lon1:(float)newLon1
lat2:(float)newLat2
lon2:(float)newLon2;
- (float)distance;
- (float)toKilometers;
- (float)toMeters;
Expand Down
27 changes: 8 additions & 19 deletions Haversine.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,23 @@
float const HAVERSINE_M_PER_KM = 1000.0;
float const HAVERSINE_F_PER_MI = 5282.0;


@implementation Haversine

@synthesize lat1, lon1, lat2, lon2;

- (id)init {
return [self initWithLat1:0.0 lon1:0.0 lat2:0.0 lon2:0.0];
}

- (id)initWithLat1:(float)newLat1
lon1:(float)newLon1
lat2:(float)newLat2
lon2:(float)newLon2 {
- (id)initWithPoint1:(CLLocationCoordinate2D)point1 andPoint2:(CLLocationCoordinate2D)point2 {
self = [super init];
if (self) {
self.lat1 = newLat1;
self.lon1 = newLon1;
self.lat2 = newLat2;
self.lon2 = newLon2;

self.point1 = point1;
self.point2 = point2;
}
return self;
}

- (float)distance {
float lat1Rad = lat1 * HAVERSINE_RADS_PER_DEGREE;
float lat2Rad = lat2 * HAVERSINE_RADS_PER_DEGREE;
float dLonRad = ((lon2 - lon1) * HAVERSINE_RADS_PER_DEGREE);
float dLatRad = ((lat2 - lat1) * HAVERSINE_RADS_PER_DEGREE);
float lat1Rad = self.point1.latitude * HAVERSINE_RADS_PER_DEGREE;
float lat2Rad = self.point2.latitude * HAVERSINE_RADS_PER_DEGREE;
float dLonRad = ((self.point2.longitude - self.point1.longitude) * HAVERSINE_RADS_PER_DEGREE);
float dLatRad = ((self.point2.latitude - self.point1.latitude) * HAVERSINE_RADS_PER_DEGREE);
float a = pow(sin(dLatRad / 2), 2) + cos(lat1Rad) * cos(lat2Rad) * pow(sin(dLonRad / 2), 2);
return (2 * atan2(sqrt(a), sqrt(1 - a)));
}
Expand Down