Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' of github.com:drewcrawford/BriefcaseWars2

  • Loading branch information...
commit dffe451791bd73d5fd6e19293fd2847074682b7b 2 parents db3dc17 + 79d0229
@drewcrawford authored
Showing with 40 additions and 8 deletions.
  1. +40 −8 BriefcaseWars2/BW2NavigateViewController.m
View
48 BriefcaseWars2/BW2NavigateViewController.m
@@ -42,33 +42,65 @@ - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray
self.myloc1.text = [NSString stringWithFormat:@"N %@",[self stringFromCoord:latestLocation.coordinate.latitude]];
self.myloc2.text = [NSString stringWithFormat:@"W %@",[self stringFromCoord:-1 * latestLocation.coordinate.longitude]];
- self.distance.text = [NSString stringWithFormat:@"%.2f mi", [latestLocation distanceFromLocation:destination] * 0.000621371];
+ self.distance.text = [NSString stringWithFormat:@"%.2f mi", [latestLocation distanceFromLocation:destination] * 0.000621371]; //conversion metric -> english
- self.accuracy.text = [NSString stringWithFormat:@"%d ft",(int)(latestLocation.horizontalAccuracy *0.3048)];
+ self.accuracy.text = [NSString stringWithFormat:@"%d ft",(int)(latestLocation.horizontalAccuracy *0.3048)]; //conversion metric -> english
[self fixNeedle];
}
+#define DEG_FROM_RAD(rad) (rad) * 180 / M_PI
+#define RAD_FROM_DEG(deg) (deg) * M_PI / 180
+
-(void) fixNeedle {
dispatch_async(dispatch_get_main_queue(), ^{
- self.NSEW.transform = CGAffineTransformMakeRotation(-1 * lastHeading.trueHeading * 0.0174532925);
+ double alpha = RAD_FROM_DEG(360 - lastHeading.trueHeading);
+ self.NSEW.transform = CGAffineTransformMakeRotation(alpha);
/**
heading
--->[us]------>
\
- \ (shouldBeHeading)
- \
- [cache] */
+ \ (shouldBeHeading)
+ \
+ [cache] */
//I think we start at the NSEW transform
- self.needle.transform = self.NSEW.transform;
-
+ double lat1 = lastLocation.coordinate.latitude;
+ double lon1 = lastLocation.coordinate.longitude;
+ double lat2 = destination.coordinate.latitude;
+ double lon2 = destination.coordinate.longitude;
+ double theta = headingInRadians(RAD_FROM_DEG(lat1), RAD_FROM_DEG(lon1), RAD_FROM_DEG(lat2), RAD_FROM_DEG(lon2));
+ NSLog(@"alpha = %f\ntheta = %f", DEG_FROM_RAD(alpha), DEG_FROM_RAD(theta));
+ self.needle.transform = CGAffineTransformMakeRotation(alpha + theta);
});
}
+//taken from https://github.com/progrmr/SDK_Utilities/tree/master/Utils
+double headingInRadians(double lat1, double lon1, double lat2, double lon2)
+{
+ //-------------------------------------------------------------------------
+ // Algorithm found at http://www.movable-type.co.uk/scripts/latlong.html
+ //
+ // Spherical Law of Cosines
+ //
+ // Formula: θ = atan2( sin(Δlon) * cos(lat2),
+ // cos(lat1) * sin(lat2) − sin(lat1) * cos(lat2) * cos(Δlon) )
+ // JavaScript:
+ //
+ // var y = Math.sin(dLon) * Math.cos(lat2);
+ // var x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);
+ // var brng = Math.atan2(y, x).toDeg();
+ //-------------------------------------------------------------------------
+ double dLon = lon2 - lon1;
+ double y = sin(dLon) * cos(lat2);
+ double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
+
+ return atan2(y, x);
+}
+
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
lastHeading = newHeading;
if (newHeading.trueHeading > 315 || newHeading.trueHeading < 45) {
Please sign in to comment.
Something went wrong with that request. Please try again.