6

As title said, I have current location, and want to know the bearing in degree from my current location to other location. I have read this post, is the value return by location.getBearing() my answer?

Let say it simply: from the picture, I expect the value of 45 degree. foo

Community
  • 1
  • 1
user3143433
  • 157
  • 1
  • 9
  • What does the documentation for that method tell you? This question is impossible to answers since you have not said why you think this `getBearing()` is **not** the right method. – Simon Jan 11 '14 at 09:59
  • 1
    Use float bearing = myLocation.bearingTo(otherLocation); – NickT Jan 11 '14 at 10:06

4 Answers4

4

I was having trouble doing this and managed to figure it out converting the C? approach to working out the bearing degree.

I worked this out using 4 coordinate points

Start Latitude

Start Longitude

End Latitude

End Longitude

Here's the code:

    protected double bearing(double startLat, double startLng, double endLat, double endLng){
    double latitude1 = Math.toRadians(startLat);
    double latitude2 = Math.toRadians(endLat);
    double longDiff= Math.toRadians(endLng - startLng);
    double y= Math.sin(longDiff)*Math.cos(latitude2);
    double x=Math.cos(latitude1)*Math.sin(latitude2)-Math.sin(latitude1)*Math.cos(latitude2)*Math.cos(longDiff);

    return (Math.toDegrees(Math.atan2(y, x))+360)%360;
}

Just change the variable names were needed.

Output bearing to a TextView

Hope it helped!

APP
  • 445
  • 1
  • 3
  • 16
Bob21
  • 167
  • 10
  • 1
    I have tried the same but I am getting a 90 degree offset, I have given same location for start and end which should give me 0 degrees, I also tried a location in 180 degree but same result – ateebahmed Oct 28 '18 at 13:27
3

Location have method bearingTo: float bearingTo(Location dest)

Location from = new Location(LocationManager.GPS_PROVIDER);
Location to = new Location(LocationManager.GPS_PROVIDER);
from.setLatitude(0);
from.setLongitude(0);
to.setLongitude(10);
to.setLatitude(10);

float bearingTo = from.bearingTo(to);
Vladimir
  • 31
  • 1
2

Here is the code for calculating bearing angle between two points(startPoint, endPoint):

public float CalculateBearingAngle(double startLatitude,double startLongitude, double endLatitude, double endLongitude){
    double Phi1 = Math.toRadians(startLatitude);
    double Phi2 = Math.toRadians(endLatitude);
    double DeltaLambda = Math.toRadians(endLongitude - startLongitude);

    double Theta = atan2((sin(DeltaLambda)*cos(Phi2)) , (cos(Phi1)*sin(Phi2) - sin(Phi1)*cos(Phi2)*cos(DeltaLambda)));
    return (float)Math.toDegrees(Theta);
}

Call for function:

float angle = CalculateBearingAngle(startLatitude, startLongitude, endLatitude, endLongitude);
xskxzr
  • 12,442
  • 12
  • 37
  • 77
APP
  • 445
  • 1
  • 3
  • 16
0

I found best method for calculating "Bearing between two point " from this answer and convert it to java from python. And this method work like charm for me!

here is the code:

    public static double getBearing(double startLat, double startLng, double endLat, double endLng) {

    double latitude1 = Math.toRadians(startLat);
    double longitude1 = Math.toRadians(-startLng);
    double latitude2 = Math.toRadians(endLat);
    double longitude2 = Math.toRadians(-endLng);

    double dLong = longitude2 - longitude1;

    double dPhi = Math.log(Math.tan(latitude2 / 2.0 + Math.PI / 4.0) / Math.tan(latitude1 / 2.0 + Math.PI / 4.0));
    if (abs(dLong) > Math.PI)
        if (dLong > 0.0)
            dLong = -(2.0 * Math.PI - dLong);
        else
            dLong = (2.0 * Math.PI + dLong);

   return  (Math.toDegrees(Math.atan2(dLong, dPhi)) + 360.0) % 360.0;
}
Fereshteh Naji
  • 104
  • 1
  • 11