Latitude/Longitude distance in Java

public class LongLatUtils
{

/**

* Calculates the great circle distance between two points on the Earth. Uses the Haversine Formula.

*

* @param latitude1 Latitude of first location in decimal degrees.

* @param longitude1 Longitude of first location in decimal degrees.

* @param latitude2 Latitude of second location in decimal degrees.

* @param longitude2 Longitude of second location in decimal degrees.

* @return Distance in meter.

*/

public static double distance( double latitude1, double longitude1, double latitude2, double longitude2 )

{

double latitudeSin = Math.sin( Math.toRadians(latitude2 - latitude1) / 2 );

double longitudeSin = Math.sin( Math.toRadians(longitude2 - longitude1) / 2 );



double a = latitudeSin * latitudeSin

+ Math.cos( Math.toRadians(latitude1)) * Math.cos(Math.toRadians(latitude2) ) * longitudeSin * longitudeSin;



double c = 2 * Math.atan2( Math.sqrt(a), Math.sqrt(1 - a) );



return 6378137 * c;

}





/**

* Converts latitude and longitude from degrees, minutes, and seconds in decimal degrees.

*

* @param degrees

* @param minutes

* @param seconds

* @return Latitude and longitude in decimal degrees.

*/

public static double convertDegreesMinutesSecondsToDecimalDegrees( int degrees, int minutes, int seconds )

{

return degrees + minutes/60. + seconds/3600.;

}



// public static void main(String[] args)

// {

// System.out.println( convertDegreesMinutesSecondsToDecimalDegrees(38, 53, 23 ));

// System.out.println( distance(38.898556, -77.037852, 38.897147, -77.043934));

// }

}

Ähnliche Beiträge

Ein Gedanke zu “Latitude/Longitude distance in Java

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert