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 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.

Haversine Formula
{\displaystyle \operatorname {hav} \left(\Theta \right)=\operatorname {hav} \left(\varphi _{2}-\varphi _{1}\right)+\cos \left(\varphi _{1}\right)\cos \left(\varphi _{2}\right)\operatorname {hav} \left(\lambda _{2}-\lambda _{1}\right)}
    public static double coordinates (double LatOne, 
                     double LatTwo, double LonOne, 
                                  double LonTwo) 

        // toRadians function converts degrees to radians. 
        LonOne = Math.toRadians(LonOne); 
        LonTwo = Math.toRadians(LonTwo); 
        LatOne = Math.toRadians(LatOne); 
        LatTwo = Math.toRadians(LatTwo); 

        // 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"); 
You are 4623.74 Miles Away

Android Studio Loading Animation Between Activities

Progress Dialog is dialog showing a progress indicator and an optional text message or view. The methods of Progress Dialog being used in this tutorial are: ProgressDialog.setTitle() – Used to set title of dialog box ProgressDialog.setMessage() – Used to set dialog message being displayed ProgressDialog.setProgressStyle() – Choose the style of indicator ProgressDialog.dismiss() – Dismiss the…

Android Studio Tutorial SeekBar

SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch thumb and drag left or right to set the current progress level or various other task. In this example, the seekbar will be used to display a percentage. As the user moves the SeekBar left to right the percentage value…

Python Ethereum Block Chain Interaction with Web3

This tutorial will show how to interact with the Ethereum blockchain using Python. To start, the Python library Web3 will need to be installed. The Web3 library allows you to interact with a local or remote Ethereum node, using a HTTP or IPC connection. Using Web3 you will be able to create smart contracts on…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s