Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /************************************************************************************************************************************                                                              
      This routine calculates the distance between two points (given the    
      latitude/longitude of those points). It is being used to calculate     
      the distance between two locations
                                                                                     
      Definitions:                                                          
    South latitudes are negative, east longitudes are positive           
                                                                                     
      Passed to function:                                                    
    lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees)  
    lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees)  
    unit = the unit you desire for results                               
           where: 'M' is statute miles                                   
                  'K' is kilometers (default)                            
                  'N' is nautical miles                                 
                          
     *************************************************************************************************************************************/
    public static double Distance(double lat1, double lon1, double lat2, double lon2, String unit) {
        double radius = 6371.0090667;
        lat1 = lat1 * Math.PI / 180.0;
        lon1 = lon1 * Math.PI / 180.0;
        lat2 = lat2 * Math.PI / 180.0;
        lon2 = lon2 * Math.PI / 180.0;
        double dlon = lon1 - lon2;

        double distance = Math
                .acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(dlon)) * radius;
        if (unit == "K") {
            return distance;
        } else if (unit == "M") {
            return (distance * 0.621371192);
        } else if (unit == "F") { //FEET
            return ((distance * 0.621371192) * 5280);
        } else if (unit == "N") {
            return (distance * 0.539956803);
        } else {
            return 0;
        }
    }
}