Here you can find the source of distance(double lat1, double lon1, double lat2, double lon2)
public static double distance(double lat1, double lon1, double lat2, double lon2)
//package com.java2s; public class Main { public static double distance(double lat1, double lon1, double lat2, double lon2) { final int R = 6371; // Radius of the earth if (lat1 == 0.0 || lon1 == 0.0 || lat2 == 0.0 || lon2 == 0.0) return 0.0; double latDistance = (lat2 - lat1); double lonDistance = (lon2 - lon1); double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) + Math.cos((lat1)) * Math.cos((lat2)) * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2); double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); double distance = R * c * 1000; // convert to meters return distance; }/*from w w w. j a va 2 s .c om*/ }