Here you can find the source of distanceBetweenTwoPoints(double lat1, double lng1, double lat2, double lng2)
public static double distanceBetweenTwoPoints(double lat1, double lng1, double lat2, double lng2)
//package com.java2s; public class Main { public static double distanceBetweenTwoPoints(double lat1, double lng1, double lat2, double lng2) { int r = 6371; // average radius of the earth in km double dLat = Math.toRadians(lat2 - lat1); double dLon = Math.toRadians(lng2 - lng1); double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2); double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); double d = r * c; return d; }/*from w w w. ja va2 s . c o m*/ }