Calculate bearing between two locations (lat, long)
Solution 1
Calculate bearing
//Source
JSONObject source = step.getJSONObject("start_location");
double lat1 = Double.parseDouble(source.getString("lat"));
double lng1 = Double.parseDouble(source.getString("lng"));
// destination
JSONObject destination = step.getJSONObject("end_location");
double lat2 = Double.parseDouble(destination.getString("lat"));
double lng2 = Double.parseDouble(destination.getString("lng"));
double dLon = (lng2-lng1);
double y = Math.sin(dLon) * Math.cos(lat2);
double x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
double brng = Math.toDegrees((Math.atan2(y, x)));
brng = (360 - ((brng + 360) % 360));
Convert Degrees into Radians
Radians = Degrees * PI / 180
Convert Radians into Degrees
Degrees = Radians * 180 / PI
Solution 2
I know this question is old, but here is an easier solution:
float bearing = loc1.bearingTo(loc2);
Solution 3
In the formula
float possibleAzimuth = (M_PI * .5f) - atan(latitudinalDifference / longitudinalDifference);
the term (M_PI * .5f)
means π/2 which is 90°. That means that it is the same formula that you stated at first, because regarding to the figure above it holds
β = arctan (a/b) = 90° - arctan(b/a).
So both formulas are similar if a
refers to the difference in longitude and b
in the difference in latitude. The last formula calculates again the same using the first part of my equation.
Solution 4
Try this for accurate result:
private static double degreeToRadians(double latLong) {
return (Math.PI * latLong / 180.0);
}
private static double radiansToDegree(double latLong) {
return (latLong * 180.0 / Math.PI);
}
public static double getBearing() {
//Source
JSONObject source = step.getJSONObject("start_location");
double lat1 = Double.parseDouble(source.getString("lat"));
double lng1 = Double.parseDouble(source.getString("lng"));
// destination
JSONObject destination = step.getJSONObject("end_location");
double lat2 = Double.parseDouble(destination.getString("lat"));
double lng2 = Double.parseDouble(destination.getString("lng"));
double fLat = degreeToRadians(lat1);
double fLong = degreeToRadians(lng1);
double tLat = degreeToRadians(lat2);
double tLong = degreeToRadians(lng2);
double dLon = (tLong - fLong);
double degree = radiansToDegree(Math.atan2(sin(dLon) * cos(tLat),
cos(fLat) * sin(tLat) - sin(fLat) * cos(tLat) * cos(dLon)));
if (degree >= 0) {
return degree;
} else {
return 360 + degree;
}
}
You can test bearing result on http://www.sunearthtools.com/tools/distance.php .
Solution 5
a
in the diagram is the longitude difference, b
is the latitude difference therefore in the method you have written you've got them the wrong way round.
a = destination.latitude - user.latitude; // should be b
b = destination.longitude - user.longitude; // should be a
Try switching them and see what happens.
See Palund's response for answers to the rest of your questions.
Related videos on Youtube
![VansFannel](https://i.stack.imgur.com/Yw9Lg.png?s=256&g=1)
VansFannel
I'm software architect, entrepreneur and self-taught passionate with new technologies. At this moment I am studying a master's degree in advanced artificial intelligence and (in my free time ) I'm developing an immersive VR application with Unreal Engine. I have also interested in home automation applying what I'm learning with Udacity's nanodegree course in object and voice recognition.
Updated on December 21, 2021Comments
-
VansFannel over 2 years
I'm trying to develop my own augmented reality engine.
Searching on internet, I've found this useful tutorial. Reading it I see that the important thing is bearing between user location, point location and north.
The following picture is from that tutorial.
Following it, I wrote an Objective-C method to obtain beta:
+ (float) calculateBetaFrom:(CLLocationCoordinate2D)user to:(CLLocationCoordinate2D)destination { double beta = 0; double a, b = 0; a = destination.latitude - user.latitude; b = destination.longitude - user.longitude; beta = atan2(a, b) * 180.0 / M_PI; if (beta < 0.0) beta += 360.0; else if (beta > 360.0) beta -= 360; return beta; }
But, when I try it, it doesn't work very well.
So, I checked iPhone AR Toolkit, to see how it works (I've been working with this toolkit, but it is so big for me).
And, in ARGeoCoordinate.m there is another implementation of how to obtain beta:
- (float)angleFromCoordinate:(CLLocationCoordinate2D)first toCoordinate:(CLLocationCoordinate2D)second { float longitudinalDifference = second.longitude - first.longitude; float latitudinalDifference = second.latitude - first.latitude; float possibleAzimuth = (M_PI * .5f) - atan(latitudinalDifference / longitudinalDifference); if (longitudinalDifference > 0) return possibleAzimuth; else if (longitudinalDifference < 0) return possibleAzimuth + M_PI; else if (latitudinalDifference < 0) return M_PI; return 0.0f; }
It uses this formula:
float possibleAzimuth = (M_PI * .5f) - atan(latitudinalDifference / longitudinalDifference);
Why is (M_PI * .5f) in this formula? I don't understand it.
And continue searching, I've found another page talking about how to calculate distance and bearing of 2 locations. In this page there is another implementation:
/** * Returns the (initial) bearing from this point to the supplied point, in degrees * see http://williams.best.vwh.net/avform.htm#Crs * * @param {LatLon} point: Latitude/longitude of destination point * @returns {Number} Initial bearing in degrees from North */ LatLon.prototype.bearingTo = function(point) { var lat1 = this._lat.toRad(), lat2 = point._lat.toRad(); var dLon = (point._lon-this._lon).toRad(); 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); return (brng.toDeg()+360) % 360; }
Which one is the right one?
-
Dolbz over 12 yearsYou refer to
a
as difference in lat andb
as difference in long. See my response for howa
should actually the difference in long andb
the difference in lat. It might be useful to edit your answer to be consistent with the diagram so we don't confuse future readers any more than necessary? -
Palund over 12 yearsThank you, Dolbz, you are right, a and b should be changed. I edited my main answer in order to have a right one. :)
-
VansFannel over 12 yearsThanks. I'm seeing now that you have worked with this ar browser. Could you help me to find which class containing bearing calculations?
-
Daniele over 12 yearsHi, I'm not the developer of the iPhone version but according to the class documentation available at: code.google.com/p/mixare/wiki/FileResponsibility it swhould be class AugmentedViewController. Source is: github.com/mixare/mixare-iphone/blob/master/Classes/gui/…
-
ravemir about 11 yearsYou convert the value to degrees, but isn't Latitude and Longitude in degrees? Shouldn't you convert them to radians first?
-
Kirit Vaghela about 11 yearsI dont convert the latitude and Longitude in radius. I don't think that it make any difference
-
Vishal Jadav over 7 yearsDo you tried online bearing match for testing? for ex. test on by this link. sunearthtools.com/tools/distance.php
-
ManEatingCheese over 5 yearsI've implemented the math portion of this (starting with double dLon =) and when I calculate reciprocal headings, the results aren't +/- 180*, but rather +/- 180* +/- 0.25*. Am I the only one seeing this issue? Is it just how the math is (imprecise) or did I do something wrong?