Here you can find the source of distance(double latDegrees1, double lonDegrees1, double latDegrees2, double lonDegrees2)
public static double distance(double latDegrees1, double lonDegrees1, double latDegrees2, double lonDegrees2)
//package com.java2s; public class Main { public static final double c_wgs84_a = 6378137.0; public static final double c_wgs84_e2 = 0.00669437999013; public static double distance(double latDegrees1, double lonDegrees1, double latDegrees2, double lonDegrees2) { double[] offsets = WGS84displacement(latDegrees1, lonDegrees1, 0, latDegrees2, lonDegrees2, 0); return Math.hypot(offsets[0], offsets[1]); }/*from ww w. jav a2 s. c om*/ /** * Copied from Dune * * @param loc1 * @param loc2 * @return */ public static double[] WGS84displacement(double latDegrees1, double lonDegrees1, double depth1, double latDegrees2, double lonDegrees2, double depth2) { double cs1[]; double cs2[]; cs1 = toECEF(latDegrees1, lonDegrees1, depth1); cs2 = toECEF(latDegrees2, lonDegrees2, depth2); double ox = cs2[0] - cs1[0]; double oy = cs2[1] - cs1[1]; double oz = cs2[2] - cs1[2]; double[] lld1 = { latDegrees1, lonDegrees1, depth1 }; double slat = Math.sin(Math.toRadians(lld1[0])); double clat = Math.cos(Math.toRadians(lld1[0])); double slon = Math.sin(Math.toRadians(lld1[1])); double clon = Math.cos(Math.toRadians(lld1[1])); double[] ret = new double[3]; ret[0] = -slat * clon * ox - slat * slon * oy + clat * oz; // North ret[1] = -slon * ox + clon * oy; // East ret[2] = depth1 - depth2; return ret; } /** * Copied from Dune * * @param loc * @return */ private static double[] toECEF(double latDegrees, double lonDegrees, double depth) { double lld[] = { latDegrees, lonDegrees, depth }; lld[0] = Math.toRadians(lld[0]); lld[1] = Math.toRadians(lld[1]); double cos_lat = Math.cos(lld[0]); double sin_lat = Math.sin(lld[0]); double cos_lon = Math.cos(lld[1]); double sin_lon = Math.sin(lld[1]); double rn = c_wgs84_a / Math.sqrt(1.0 - c_wgs84_e2 * sin_lat * sin_lat); double[] ned = new double[3]; ned[0] = (rn - lld[2]) * cos_lat * cos_lon; ned[1] = (rn - lld[2]) * cos_lat * sin_lon; ned[2] = (((1.0 - c_wgs84_e2) * rn) - lld[2]) * sin_lat; return ned; } }