Here you can find the source of distance(double lon1, double lat1, double lon2, double lat2)
public static double distance(double lon1, double lat1, double lon2, double lat2)
//package com.java2s; public class Main { public static double distance(double lon1, double lat1, double lon2, double lat2) { double radLat1 = rad(lat1); double radLat2 = rad(lat2); double a = radLat1 - radLat2; double b = rad(lon1) - rad(lon2); double c = 2.0D * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2.0D), 2.0D) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2.0D), 2.0D))); return c * 6371004.0D; }//from w ww .j a v a 2 s.c o m private static double rad(double d) { return d * 3.141592653589793D / 180.0D; } }