This is the code for calculating the distance between two given points by their latitude and longitude values.
The formula was taken from here.
public static double getOrthodromicDistance( double lat1, double lon1, double lat2, double lon2) { double R = 6371.0; // km double dLat = convertDegreeToRadians((lat2 - lat1)); double dLon = convertDegreeToRadians((lon2 - lon1)); lat1 = convertDegreeToRadians(lat1); lat2 = convertDegreeToRadians(lat2); double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * MathUtilities.atan2(Math.sqrt(a), Math.sqrt(1 - a)); double distance = R * c; return distance; }
The formula was taken from here.
The MathUtilities.atan2() method is found only in BlackBerry API 4.6 or higher See The definition of atan2 here
This can also be calculated like this:
This can also be calculated like this:
double atan2t(double y, double x) { double lo, hi, res, val, resval; double ERR = 1e-9; if (Math.abs(x) < ERR) { if (Math.abs(y) >= ERR) { return ((y / Math.abs(y)) * PI / 2.0); } else { return (0.0); } } val = Math.abs(y / x); lo = 0.0; hi = PI / 2; res = (lo + hi) / 2.0; resval = Math.tan(res); while (Math.abs(hi - lo) > ERR) { if (resval > val) { hi = res; } else { lo = res; } res = (hi + lo) / 2.0; resval = Math.tan(res); } if (x < 0.0) { res = PI - res; } if (Math.abs(y) >= ERR) { return ((y / Math.abs(y)) * res); } else { return res; } }
No comments:
Post a Comment