2012-02-03 141 views
1

如何計算JSP中兩個位置之間的距離。我有兩個位置的座標,我只想知道在JSP中是否有任何可用的功能。在jsp中調整兩個位置之間的距離

在Android中,我使用這樣的功能,計算兩兩地之間的距離:

Location.distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude, results); 

如何能夠做到在JSP中這樣的事情。有沒有任何jar文件,我需要爲此添加。請幫助我。

+0

你知道如何計算它在Java中,不知道如何端口這個解決方案JSP ,或者你不知道如何在普通的Java SE中做到這一點? – 2012-02-03 11:11:51

回答

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; 
} 

}

+0

我認爲您提供的第二個鏈接中的第一個功能就足夠了。 – 2012-02-03 11:04:14

相關問題