Calculate great circle distance
arcgissamples\geometry\CalculateGreatCircleDistance.java
/* Copyright 2010 ESRI
* 
* All rights reserved under the copyright laws of the United States
* and applicable international laws, treaties, and conventions.
* 
* You may freely redistribute and use this sample code, with or
* without modification, provided you include the original copyright
* notice and use restrictions.
* 
* See the use restrictions.
* 
*/
package arcgissamples.geometry;

public class CalculateGreatCircleDistance
{
  private static double EARTH_CIRC_METERS = 40030218; // Radius = 6371007 (GRS80)
  
  public CalculateGreatCircleDistance()
  {
    
  }

  public static void main(String[] args)
  {
    CalculateGreatCircleDistance d = new CalculateGreatCircleDistance();
    //34.050274, -117.171316 - Redlands, CA
    //41.879535, -87.624333 - Chicago, IL
    System.out.println("Calculated Great Circle Distance: " + d.calculateDistance(34.050274, -117.171316, 41.879535, -87.624333));
  }

  /**
   * Calculates distances between 2 points
   * 
   * @param lat1 - Latitude of origin point in decimal degrees
   * @param lon1 - longitude of origin point in deceimal degrees
   * @param lat2 - latitude of destination point in decimal degrees
   * @param lon2 - longitude of destination point in decimal degrees
   * @return metricDistance - great circle distance in meters
   */
  public double calculateDistance(double lat1, double lon1, double lat2, double lon2)
  {
    double radLat1 = Math.toRadians(lat1);
    double radLon1 = Math.toRadians(lon1);
    double radLat2 = Math.toRadians(lat2);
    double radLon2 = Math.toRadians(lon2);

    double d = Math.acos((Math.cos(radLat1) * Math.cos(radLat2)) + (Math.sin(radLat1) * Math.sin(radLat2))
        * (Math.cos(radLon1 - radLon2)));
    return (d * EARTH_CIRC_METERS);
  }
}