by dmt » Thu Jan 08, 2009 1:32 am
Are you talking about computing the distance between 2 points on the surface of a sphere?
If so, you'll need to brush up on your trigonometry, and first compute the Haversine function (this is the standard way of doing it). In order to use the Java trig functions, you'll have to first convert all your angles from degrees to radians.
Given two longitude/latitude pairs, and the earth's average radius (assume 6356.78km for your calculations),
you can calculate the distance between the 2 points via this Java code:
double EarthRad = 6356.78; // in km !
// first convert to radians...
double geo1_lat = geo1.getLatitude()*java.lang.Math.PI/360;
double geo1_lng = geo1.getLongitude()*java.lang.Math.PI/360;
double geo2_lat = geo2.getLatitude()*java.lang.Math.PI/360;
double geo2_lng = geo2.getLongitude()*java.lang.Math.PI/360;
double deltaLat = java.lang.Math.abs(java.lang.Math.abs(geo2_lat) - java.lang.Math.abs(geo1_lat));
double deltaLng = java.lang.Math.abs(java.lang.Math.abs(geo2_lng) - java.lang.Math.abs(geo1_lng));
double dist = 2*EarthRad*java.lang.Math.asin(java.lang.Math.sqrt(haversine(deltaLat)
+ java.lang.Math.cos(pair1_lat)
*java.lang.Math.cos(pair1_lng)*haversine(deltaLng)));
Where "dist" now contains the distance between along the earth's surface.
You can find the Haversine function trig equation by Googling it, then construct a method that returns the appropriate value.