I am creating an app that will point a simple arrow in the direction of a GPS coordinate I have plotted out. So I am talking my current location lat and long, so lat1, lon1 is the current location... then comparing them against a static coordinate of lat and long called lat2, lon2.
I have spent HOURS researching and testing, but I keep on getting the same problem. It's very aggravating. As soon as I start to drive towards my destination the arrow is all out of whacked when using the iPhone's built in GPS/compass. It will sometimes show the arrow the complete opposite side of where it needs to be, hanging to the right... pretty much nothing that is solid.
Here is the current code I am running
cpLocLat = 43.026629;
cpLocLon = -78.867188;
Those are float values and the coordinates of where I want to drive to.
float lat1 = self.locManager.location.coordinate.latitude;
float lat2 = cpLocLat;
float lon1 = self.locManager.location.coordinate.longitude;
float lon2 = cpLocLon;
float heading = atan2(sin(lon2 - lon1) * cos(lat2), cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(lon2 - lon1));
heading = RADIANS_TO_DEGREES(heading);
if (heading < 0) {
heading = heading + 360;
}
I use this function every time the locationManager didUpdateToLocation delagate is called. Once I have that information, it is used in the locationManager didUpdateHeading delagte with the following:
CGAffineTransform where = CGAffineTransformMakeRotation(degreesToRadians((degrees - newHeading.magneticHeading)));
[self.compassContainer2 setTransform:where];
and here is the degressToRadians macro
#define degreesToRadians(x) (M_PI * x / 180.0)
So whenever I run this code it is failing epically and is making me go nuts!
BTW - I have used the following URLs for reference
http://www.shawngrimes.me/2011/07/calculating-heading-with-corelocation/
Using Compass to point to actual coordinate location instead of just North