get Degrees Between Two Points on earth - Android Map

Android examples for Map:Location

Description

get Degrees Between Two Points on earth

Demo Code


//package com.java2s;

public class Main {
    public static Integer getDegreesBetweenTwoPoints(Double latitudP1,
            Double longitudP1, Double latitudP2, Double longitudP2) {
        /*double y = Math.sin(longitudP2 - longitudP1) * Math.cos(latitudP2);
        double x = Math.cos(latitudP1) * Math.sin(latitudP2) - Math.sin(latitudP1) * Math.cos(latitudP2) * Math.cos(longitudP2 - longitudP1);
        Double bearing = Math.toDegrees(Math.atan2(y, x));
        bearing = (bearing + 360) % 360;
        //bearing = (bearing + 180) % 360;
        return bearing.intValue();*/

        double latitude1 = Math.toRadians(latitudP1);
        double latitude2 = Math.toRadians(latitudP2);
        double longDiff = Math.toRadians(longitudP2 - longitudP1);
        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);

        // Double bearing = (Math.toDegrees(Math.atan2(y, x)) + 360)%360;
        Double bearing = (Math.toDegrees(Math.atan2(y, x)) + 360) % 360;
        return bearing.intValue();
    }// w w w  . j  av a 2 s.c  o  m
}

Related Tutorials