# Java Tutorial : Distance Between two Places in Miles

This tutorial will show to to get the distance between two places using Java programming. First, to start this project we need the Longitude and Latitude from two separate locations. For this example, I used Random.org to find two different GPS coordinates.

Once the coordinates are chosen, next have to convert those coordinates into radiants. This is done by using to toRadians function that is built into the Java Math package. The Longitude and Latitude of BOTH coordinates have to be to converted before they can be placed in the formula.

The formula being used for this project is the Haversine formula. The Haversine formula is used to find the distance between to point on a sphere. Note, the Earth has a radius of 3,956 and because the we are trying to find difference in miles between two points, we must multiple the formula output by the Earths’s radius.

    public static double coordinates (double LatOne,
double LatTwo, double LonOne,
double LonTwo)
{

// Here the Haversine formula is being created
double deltaLon = LonTwo - LonOne;
double deltaLat = LatTwo - LatOne;
double formula = Math.pow(Math.sin(deltaLat / 2), 2)
+ Math.cos(LatOne) * Math.cos(LatTwo)
* Math.pow(Math.sin(deltaLon / 2),2);
double fOutput = 2 * Math.asin(Math.sqrt(formula));

// Earth's Radius multiplied by OP
double r = 3956;
return(fOutput * r);

}
public static void main(String[] args)

{

// Input First destination Lon & Lat
double LatOne =  -60.25901;
double LonOne = -12.82658;

// Input Second destination Lon & Lat
double LatTwo =  -47.01424;
double LonTwo = -148.97184;

//Printing output
System.out.println("You are " + coordinates (LatOne, LatTwo,
LonOne, LonTwo)+ " Miles Away");
}
}

##### Output
You are 4623.74 Miles Away