Here you can find the source of calculateDistanceInKm(double lat1, double lon1, double lat2, double lon2)
public static double calculateDistanceInKm(double lat1, double lon1, double lat2, double lon2)
//package com.java2s; //License from project: Apache License public class Main { public static double calculateDistanceInKm(double lat1, double lon1, double lat2, double lon2) { int R = 6371; // Radius of the earth in km double dLat = deg2rad(lat2 - lat1); // deg2rad below double dLon = deg2rad(lon2 - lon1); double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(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; // Distance in km return d; }//from w w w . j av a 2 s.c o m private static double deg2rad(double deg) { return deg * (Math.PI / 180); } }