3
我從http://www.codereye.com/2010/10/how-to-calculate-distance-between-2.html得到了這段代碼,它根據它們相應的緯度和經度計算兩個IP地址之間的近似物理距離。java中兩個地理位置之間的距離:NaN
public static double distance(double lat1, double lon1, double lat2, double lon2)
{
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
return (dist);
}
lat和long是帶符號的值。然而這個代碼有時會返回dist作爲NaN。
我錯過了什麼? theta需要是絕對值嗎?
UPDATE:其中返回NaN 測試值: -
LAT1 = -23.5477,lon1 = -46.6358,LAT2 = -23.5477,lon2 = -46.6358
LAT1 = 53.3331,lon1 = -6.2489,lat2 = 53.3331,lon2 = -6.2489
一些測試數據可以幫助 – MadProgrammer
你甚至嘗試任何類型的調試解決這個問題喃?也許在一路上打印出價值? – Constant
我將用測試數據更新問題。 – Karan