Here you can find the source of distance(double latA, double logA, double latB, double logB)
public static double distance(double latA, double logA, double latB, double logB)
//package com.java2s; public class Main { private static final double PI = 3.14159265358979324D; public static double distance(double latA, double logA, double latB, double logB) { double earthR = 6371000; double x = Math.cos(latA * Math.PI / 180) * Math.cos(latB * Math.PI / 180) * Math.cos((logA - logB) * Math.PI / 180); double y = Math.sin(latA * Math.PI / 180) * Math.sin(latB * Math.PI / 180); double s = x + y; if (s > 1) s = 1;//from w w w. j ava 2s . co m if (s < -1) s = -1; double alpha = Math.acos(s); double distance = alpha * earthR; return distance; } }