我尝试使用纬度和经度计算从 Destination1 到 Destination2 的公里距离
目的地1(宿雾市PH) 的纬度为:1017.8590,经度:12353.4304
目的地2(Talisay PH) 的纬度为:1015.6445,经度:12350.0404
我的输出是: 299.28607720457882 KM
CODE:
double lat1 = Convert.ToDouble(txtLat1.Text.ToString());
double lat2 = Convert.ToDouble(txtLat2.Text.ToString());
double lon1 = Convert.ToDouble(txtLong1.Text.ToString());
double lon2 = Convert.ToDouble(txtLong2.Text.ToString());
var R = 6378.137; // Radius of earth in KM
var dLat = (lat2 - lat1) * Math.PI / 180;
var dLon = (lon2 - lon1) * Math.PI / 180;
var a =
Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
Math.Cos(lat1 * Math.PI / 180) * Math.Cos(lat2 * Math.PI / 180) *
Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
string x = (R * c).ToString();
var d = (R * c);
我尝试使用http://www.entfernungsrechner.net/en/distance/city/1717512/city/1683881此网站计算该目的地的公里数,输出 12.2 km
答案 0 :(得分:1)
嗯,对于初学者来说,你会开车12.2KM。这与您上面的Haversine公式不会产生相同的输出。
你应该尝试将小数点放在正确的位置。我得到了4.45公里更合理的结果。
https://dotnetfiddle.net/cs5Ppq
public class Program
{
public static void Main()
{
double lat1 = 10.178590;
double lat2 = 10.156445;
double lon1 = 123.500404;
double lon2 = 123.534304;
var R = 6378.137; // Radius of earth in KM
var dLat = (lat2 - lat1) * Math.PI / 180;
var dLon = (lon2 - lon1) * Math.PI / 180;
var a =
Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
Math.Cos(lat1 * Math.PI / 180) * Math.Cos(lat2 * Math.PI / 180) *
Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
string x = (R * c).ToString();
var d = (R * c);
Console.WriteLine(d);
}
}
注意:您可以根据此http://andrew.hedges.name/experiments/haversine/工具
查看结果编辑:如果你想要最准确的数字,因为Haversine公式可以低估和高估地球极端的距离,你想要使用Vincety解决方案。这不适合内心的弱者: http://www.movable-type.co.uk/scripts/latlong-vincenty.html