how to get angle using two latitude and longitude

552 views Asked by At

i have an app in which i want to do some custom compass functionality suppose i have location A and location B i mark my location (Location A) then i move to some distance my compass should point to Location A wherever i can move i am referencing qiblacompass functionality but its not pointing to location A when i move to other location here is my code

public static double getDirectionFromNorth(double degLatitude,
                double degLongitude) {


 final double direction_LATITUDE = Math.toRadians(angle_lat);
 double direction_LONGITUDE = Math.toRadians(angle_lng);

            double latitude2 = Math.toRadians(degLatitude);
            double longitude = Math.toRadians(degLongitude);

            double soorat = Math.sin(direction_LONGITUDE - longitude);
            double makhraj = Math.cos(latitude2) * Math.tan(direction_LATITUDE)
                    - Math.sin(latitude2) * Math.cos(direction_LONGITUDE - longitude);
            double returnValue = Math.toDegrees(Math.atan(soorat / makhraj));

            if (latitude2 > direction_LATITUDE) {
                if ((longitude > direction_LONGITUDE || longitude < (Math
                        .toRadians(-180d) + direction_LONGITUDE))
                        && (returnValue > 0 && returnValue <= 90)) {

                    returnValue += 180;

                } else if (!(longitude > direction_LONGITUDE || longitude < (Math
                        .toRadians(-180d) + direction_LONGITUDE))
                        && (returnValue > -90 && returnValue < 0)) {

                    returnValue += 180;

                }

            }
            if (latitude2 < direction_LATITUDE) {

                if ((longitude > direction_LONGITUDE || longitude < (Math
                        .toRadians(-180d) + direction_LONGITUDE))
                        && (returnValue > 0 && returnValue < 90)) {

                    returnValue += 180;

                }
                if (!(longitude > direction_LONGITUDE || longitude < (Math
                        .toRadians(-180d) + direction_LONGITUDE))
                        && (returnValue > -90 && returnValue <= 0)) {

                    returnValue += 180;
                }

            }
            // ***
            return returnValue;
        }

where angle_lat and angle_lng is my location A latitude,longitude and degLatitude, degLongitude are those when i move it gets lat,lng from my GPS

anyone please tell me what is fault in this code or i any other method to achieve my task?

1

There are 1 answers

0
AlexWien On

Use this web page to check your code: http://www.movable-type.co.uk/scripts/latlong.html

Then when you know that you correctly calculate the direction between two lat,longs you will realize that this is not sufficient for walking speeds.

Just drive with a car or a train, well above 10km/h, and your algorithm probabyl will work. To caluclate pedestrian walking direction you need a more advanced algorithm, considering more locations of the past e.g 30meters.

But as first shot, why dont you simply take the direction deliveered by the Location attribute: It is called heading or course. Take that, it is more acurate that the direction between two lat/lons. (because the GPS chip internaly, has more information available)