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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With