# Java Implementation of Haversine Formula for distance calculation between two points

```
package com.ananth.haversine;

/**
* This is the implementation Haversine Distance Algorithm between two places
* @author ananth
Δlat = lat2− lat1
Δlong = long2− long1
a = sin²(Δlat/2) + cos(lat1).cos(lat2).sin²(Δlong/2)
c = 2.atan2(√a, √(1−a))
d = R.c
*
*/

public class HaversineDistance {

/**
* @param args
* arg 1- latitude 1
* arg 2 - latitude 2
* arg 3 - longitude 1
* arg 4 - longitude 2
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final int R = 6371; // Radious of the earth
Double lat1 = Double.parseDouble(args);
Double lon1 = Double.parseDouble(args);
Double lat2 = Double.parseDouble(args);
Double lon2 = Double.parseDouble(args);
Double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) +
Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
Double distance = R * c;

System.out.println("The distance between two lat and long is::" + distance);

}

private static Double toRad(Double value) {
return value * Math.PI / 180;
}

}
```

## 4 thoughts on “Java Implementation of Haversine Formula for distance calculation between two points”

1. Jai Biju says:

is the distance in km or miles? how to get distance in miles?

• Jordan Clay says:

2. Miguel Merino says:
• bigdatanerd says: