Change to Radian then calculate distance - Android java.lang

Android examples for java.lang:Math Trigonometric Function

Description

Change to Radian then calculate distance

Demo Code


//package com.java2s;

public class Main {
    public static final double R = 6371.004;

    public static double getDistance(double lon1, double lat1, double lon2,
            double lat2) {
        double x = changeToRad(lon1);
        double y = changeToRad(lat1);
        double a = changeToRad(lon2);
        double b = changeToRad(lat2);
        double rad = Math.acos(Math.cos(y) * Math.cos(b) * Math.cos(x - a)
                + Math.sin(y) * Math.sin(b));
        if (rad > Math.PI)
            rad = Math.PI * 2 - rad;
        return R * rad * 1000;
    }//from   w w w.j  a v  a  2s  .c  om

    public static double changeToRad(double angle) {
        return angle / 180 * Math.PI;
    }
}

Related Tutorials