Reputation: 479
i have found a great way to calculate the angle of the direction from two coordinates, and i have tried the solution in excel, it worked fine when i typed the approximately coordinates. But if i implement the code in java, i got complately different results at dxy (angle) double variables. The coordinates are fine, i double checked them. :)
double lon1 = (double)Math.round(LongitudeDouble * 1000000) / 1000000;
double lat1 = (double)Math.round(LatitudeDouble * 1000000) / 1000000;
double lon2 = 19.055954;
double lat2 = 47.569331;
***************************
double dy = (lat2 - lat1);
double dx = Math.cos(Math.PI/180*lat1)*(lon2-lon1);
double dxy = Math.atan2(dy, dx);
double degree = 180/Math.PI*dxy;
Upvotes: 1
Views: 5309
Reputation: 28767
The reason why excell deviates from your program code, is that excell uses argument order: x,y while programming languages like java use order (y,x). see the docu for atan2.
Further, you and some other posts in SO confuse mathematical angles (east = 0 counterclockwise raising) and geografical directions in degrees (compass rose) : north = 0, clockwise). after the angle calculation which delivers mathematical angles, you must transform to geo directions, but:
you use the wrong formula. Your formula works for cartesian coordinates (e.g screen points, pixels), not for spherical (lat, long). search here on SO for bearing between two coordinates
atan2
delivers an value from [-pi, pi]. but geo direction is 0-360.
by the way the correct answer:
Bearing/heading/course from lat1,lon1 to lat,lon2 = 122.0402
So the formula is partly nonsense:
Your code gives -32.04
1. So you first exchanged from-point and to-point; an angle is measured from p1 to p2.
2. -90 -32 = -122 which is the expected result. (mathematical vs. geo angles)
So you are messing up your own numbers. you typed in Excell another number than in java, or posted the wrong number: the result in your java code is -32.04 Using these numbers:
double lon1 = 19.053143;
double lat1 = 47.570518;
double lon2 = 19.055954;
double lat2 = 47.569331;
Upvotes: 4