如何计算JSP中两个位置之间的距离。我有两个位置的坐标,我只想知道在JSP中是否有任何可用的功能。
在android中,我使用这样的函数来计算两个地方之间的距离:
Location.distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude, results);
我如何在JSP中实现这样的功能。我需要为此添加任何jar文件吗?请帮助我。
答案 0 :(得分:2)
您可以使用此处的公式http://www.koordinaten.com/informations/formula.shtml, 或者从这里http://www.codecodex.com/wiki/Calculate_distance_between_two_points_on_a_globe
public class DistanceCalculator {
private double Radius;
// R = earth's radius (mean radius = 6,371km)
// Constructor
DistanceCalculator(double R) {
Radius = R;
}
public double CalculationByDistance(GeoPoint StartP, GeoPoint EndP) {
double lat1 = StartP.getLatitudeE6()/1E6;
double lat2 = EndP.getLatitudeE6()/1E6;
double lon1 = StartP.getLongitudeE6()/1E6;
double lon2 = EndP.getLongitudeE6()/1E6;
double dLat = Math.toRadians(lat2-lat1);
double dLon = Math.toRadians(lon2-lon1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
double c = 2 * Math.asin(Math.sqrt(a));
return Radius * c;
}
}