Calculate coordinate by distance and heading. How?

Hello all!

Is there any functions in CoreLocation that will allow me to calculate coordinate of the point by heading and distance?

  • For me known perfectly about geometry. The key words in my question "Is there any functions in CoreLocation", means IN CORE LOCATION in-box.

Add a Comment

Replies

Is there any functions in CoreLocation that will allow me to calculate coordinate of the point by heading and distance?

No. The only bit of spherical geometry that Core Location has is a method to report the distance between two points:

https://developer.apple.com/documentation/corelocation/cllocation/1423689-distancefromlocation

There are various difficulties with what you want to do.

Firstly, the earth is not flat. But if you're only comparing points that are nearby and you don't need a very accurate answer and you're not worried about corner-cases like the poles or the middle of the Pacific, then you can use an approximation that pretends that the earth is flat. (See Jineshsethia's comment: "a distance on the coordinate plane..." - the earth is not a plane!)

Secondly, the earth isn't even a sphere. But if you don't need super-accurate answers then you can ignore that (the error is probably less than 1 %). Distance and bearing between two points on a sphere is something you could probably implement from equations on Wikipedia if you can manage "high school maths". (I think this is what the Stack Overflow posts that you linked to are all doing.)

If you actually want the "right" answer, you need the more complicated maths to work with the "squashed sphere" that better approximates the shape of the earth. My recommendation is to use GeographicLib. https://geographiclib.sourceforge.io It seems to be high-quality, and I use it in my mapping apps. It does much more than you need, though. What you need is its functions for "geodesics". See this page: https://geographiclib.sourceforge.io/C++/doc/classGeographicLib_1_1Geodesic.html and scroll down to the example, which I have copied in part below:

    Geodesic geod(Constants::WGS84_a(), Constants::WGS84_f());
    // Alternatively: const Geodesic& geod = Geodesic::WGS84();
    {
      // Sample direct calculation, travelling about NE from JFK
      double lat1 = 40.6, lon1 = -73.8, s12 = 5.5e6, azi1 = 51;
      double lat2, lon2;
      geod.Direct(lat1, lon1, azi1, s12, lat2, lon2);
      cout << lat2 << " " << lon2 << "\n";
    }
    {
      // Sample inverse calculation, JFK to LHR
      double
        lat1 = 40.6, lon1 = -73.8, // JFK Airport
        lat2 = 51.6, lon2 = -0.5;  // LHR Airport
      double s12;
      geod.Inverse(lat1, lon1, lat2, lon2, s12);
      cout << s12 << "\n";
 }