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; /*/*from www.ja v a 2 s. c o m*/ * GeoUtils.java - Copyright(c) 2013 Joe Pasqua * Provided under the MIT License. See the LICENSE file for details. * Created: Jul 10, 2013 */ public class Main { /** * Calculate distance between two points in latitude and longitude taking * into account height difference. If you are not interested in height * difference pass 0.0. Uses Haversine method as its base. * * lat1, lon1 Start point lat2, lon2 End point */ public static double distance(double lat1, double lon1, double lat2, double lon2) { final int R = 6371; // Radius of the earth Double latDistance = deg2rad(lat2 - lat1); Double lonDistance = deg2rad(lon2 - lon1); Double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(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; } private static double deg2rad(double deg) { return (deg * Math.PI / 180.0); } }