简体   繁体   中英

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

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.富

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!

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);

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);

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM