android - how can I get the bearing degree between two locations?
Asked Answered
L

4

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

Liaoning answered 11/1, 2014 at 9:56 Comment(2)
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.Panjabi
Use float bearing = myLocation.bearingTo(otherLocation);Wheelwright
D
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!

Dieldrin answered 6/3, 2014 at 23:10 Comment(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 resultHogan
W
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);
Warfield answered 19/1, 2015 at 14:21 Comment(1)
This method is giving me 90 degree offset on same location and location having 180 degree bearingHogan
F
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);
Filet answered 16/5, 2018 at 5:53 Comment(0)
L
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;
}
Lebaron answered 27/6, 2022 at 10:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.