Here you can find the source of distanceHaversine(Double lat1, Double lon1, Double lat2, Double lon2, String param)
public static Double distanceHaversine(Double lat1, Double lon1, Double lat2, Double lon2, String param)
//package com.java2s; //License from project: Apache License public class Main { public static Double distanceHaversine(Double lat1, Double lon1, Double lat2, Double lon2, String param) { final Double earthRadius = 3960.00; Double latDistance = lat2 - lat1; Double lonDistance = lon2 - lon1; Double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) + Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2); Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); Double distance = earthRadius * c; if (param.equals("miles")) return distance; else if (param.equals("km")) return distance * 1.609344; else//from ww w . jav a 2 s . co m return (distance * 1.609344) / 1000; } private static Double toRad(Double value) { return value * Math.PI / 180; } }