javamathlatitude-longituderadiansearthdistance

How do I write a Java program that calculates the distance between two points on earth?


I know how to start it out and I know how to put in the scanners and everything, but in school, I've never really learned about longitude and latitude formulas and how to convert those points into radians. So I'm pretty much stuck on this Java problem. Here is what I have so far:

import java.util.*;

class DistanceCalculator {

    // Radius of the earth in km; this is the class constant.
    public static final double Radius = 6372.795; 

    /**
     * This program computes the spherical distance between two points on the surface of the Earth.
     */

    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        intro();

        System.out.print("Longitude (degrees.minutes) ");
        double Longitude = console.nextDouble();
        System.out.print("Latitude (degrees.minutes) ");
        double Latitude = console.nextDouble(); 
    }

    public static double distFrom(double lat1, double lng1, double lat2, double lng2); 
        double Latitude = Math.toRadians(...);  
    }

    public static void intro() {
        System.out.println("This program computes the spherical distance between two points on the surface of the Earth.");
        System.out.println("\tPlease start by entering the longitude and the latitude of location 1.");
    }
}

In Java IDE, they say that Longitude and Latitude points (the ones underneath the intro();) are not used, and I know why, since I haven't really defined them yet. I know I'm missing the formula for longitude and latitude. In my book, it wants me to use the spherical law of cosines, and since I've never learned this at school, no matter how hard I study the formula from the websites I sought out, I don't know how to transfer that into Java language.
Another problem is, how do I transfer degrees and minutes from a longitude/latitude point into radians? Do I have to use Math.toRadians thing? Oh yeah and also, my answer has to be in kilometers.

Updated: The math functions some of you guys are talking about confuses me greatly. In school (I'm a high schooler), even at Math IB SL, my teacher has never taught us how to find long/lat. points...yet. So it's hard for me to grasp. Since the spherical law of cosines formula is online, do I basically just take that formula and convert it into "java language" and plug it into my program?


Solution

  • The key word you need to search for is the "Haversine formula".

    An easier to understand method, but one which is not quite so accurate for small distances, is to recall that the angle between two vectors A and B can be calculated using the dot product:

    A ⋅ B = |A| * |B| * cos(theta)

    so if you convert your polar lat/long pairs into 3D cartesian coordinates (and yes, you'll need to use Math.toRadians(), Math.cos() and Math.sin() to do that, and then calculate the dot product, you'll then get cos(theta), so use Math.acos() to get theta.

    You can then work out the distance simply as D = R * theta, where R is the radius of the Earth, and theta remains in radians.