4

I have 2 coordinates and would like to do something seemingly straightforward. I want to figure out, given:

1) Coordinate A 2) Course provided by Core Location 3) Coordinate B

the following:

1) Distance between A and B (can currently be done using distanceFromLocation) so ok on that one. 2) The course that should be taken to get from A to B (different from course currently traveling)

Is there a simple way to accomplish this, any third party or built in API?

Apple doesn't seem to provide this but I could be wrong.

Thanks, ~Arash

EDIT:

Thanks for the fast responses, I believe there may have been some confusion, I am looking to get the course (bearing from point a to point b in degrees so that 0 degrees = north, 90 degrees = east, similar to the course value return by CLLocation. Not trying to compute actual turn by turn directions.

iOS4Life
  • 226
  • 2
  • 13

4 Answers4

4

I have some code on github that does that. Take a look at headingInRadians here. It is based on the Spherical Law of Cosines. I derived the code from the algorithm on this page.

/*-------------------------------------------------------------------------
* Given two lat/lon points on earth, calculates the heading
* from lat1/lon1 to lat2/lon2.
*
* lat/lon params in radians
* result in radians
*-------------------------------------------------------------------------*/
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(Δlong) * cos(lat2),
    // cos(lat1) * sin(lat2) − sin(lat1) * cos(lat2) * cos(Δlong) )
    // 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);
}
progrmr
  • 75,956
  • 16
  • 112
  • 147
  • Thanks progrmr, I am checking this out now! – iOS4Life Jun 19 '11 at 20:00
  • double headingInRadians(double lat1, double lon1, double lat2, double lon2) This is the method I tried and provided the following as input gives me a result of -0.343130 radian which i converted to degrees and got 340.340088. Could you verify that I did this correctly? PASSING IN THE FOLLOWING COORDINATES: lat1 = 36.868148 long1 = -76.046454 lat2 = 39.174231 long2 = -77.111107 All of these are converted to radians before going into the formula using the following equation: (value / 180) * M_PI Any help appreciated, thanks again! – iOS4Life Jun 19 '11 at 20:05
  • You can check it yourself using an online calculator such as this one [here](http://www.csgnetwork.com/gpsdistcalc.html) – progrmr Jun 19 '11 at 20:46
1

See How to get angle between two POI?

Community
  • 1
  • 1
Jano
  • 62,815
  • 21
  • 164
  • 192
  • Using this method, i get 335.218658 degrees where as the example posted here: http://www.movable-type.co.uk/scripts/latlong.html gives an initial bearing of 159.* and final bearing of 160.* Is this one correct or is that one correct? – iOS4Life Jun 19 '11 at 20:29
  • It looks like this answer is using Pythagoras’ theorem with an equirectangular projection, that is less accurate than the Spherical Law of Cosines algorithm that my answer used, but it uses less trig functions so could be faster. Read [this page](http://www.movable-type.co.uk/scripts/latlong.html) for a comparison of different algorithms. – progrmr Jun 19 '11 at 20:52
  • Seems like 159.~ is the correct initial bearing and if you are going the reverse direction then it should be a initial bearing of 340 from point b to point a so im thinking that i have this correctly implemented and that my final bearing is telling me the bearing from point b to point a (the opposite direction of travel) but still a good bearing number. Thoughts? – iOS4Life Jun 19 '11 at 22:19
0

Depending on how much work you want to put in this one, I would suggest looking at Tree Traversal Algorithms (check the column on the right), things like A* alpha star, that you can use to find your find from one point to another, even if obstacles are in-between.

c00kiemon5ter
  • 16,994
  • 7
  • 46
  • 48
0

If I understand you correctly, you have the current location and you have some other location. You want to find the distance (as the crow flies) between the two points, and to find a walking path between the points.

To answer your first question, distanceFromLocation will find the distance across the earth's surface between 2 points, that is it follows the curvature of the earth, but it will give you the distance as the crow flies. So I think you're right about that.

The second question is a much harder. What you want to do is something called path-finding. Path finding, require's not only a search algorithm that will decide on the path, but you also need data about the possible paths. That is to say, if you want to find a path through the streets, the computer has to know how the streets are connected to each other. Furthermore, if you're trying to make a pathfinder that takes account for traffic and the time differences between taking two different possible paths, you will need a whole lot more data. It is for this reason that we usually leave these kinds of tasks up to big companies, with lots of resources, like Google, and Yahoo.

However, If you're still interested in doing it, check this out http://www.youtube.com/watch?v=DoamZwkEDK0

Rich
  • 1,082
  • 7
  • 11
  • Please see edited post, I didn't mean the path to take to get there but rather the bearing in degrees to take for instance 0 degrees for north 90 for east, etc... – iOS4Life Jun 19 '11 at 19:58