C# 如何在c中计算lat/long之间的距离#

C# 如何在c中计算lat/long之间的距离#,c#,sql-server,google-maps,C#,Sql Server,Google Maps,在c#winform应用程序中,我有一个纬度/经度列表。我可以在sql server(2005)查询中进行计算,但我不知道是否可以在c#中进行计算 在sql server中使用此查询,我可以排序纬度和经度列表。 但是,在c#中,我在列表中列出了lat/long: var latLon = new List<LatLonClass>(); var latLon=new List(); 使用此列表,如何在列表中执行相同的查询? 谢谢 这是哈弗森公式的一个片段,发现(https://w

在c#winform应用程序中,我有一个纬度/经度列表。我可以在sql server(2005)查询中进行计算,但我不知道是否可以在c#中进行计算

在sql server中使用此查询,我可以排序纬度和经度列表。 但是,在c#中,我在列表中列出了lat/long:

var latLon = new List<LatLonClass>();
var latLon=new List();
使用此列表,如何在列表中执行相同的查询?
谢谢

这是哈弗森公式的一个片段,发现(
https://www.stormconsultancy.co.uk/blog/development/code-snippets/the-haversine-formula-in-c-and-sql/
)。看起来它有你需要的东西

public double HaversineDistance(LatLng pos1, LatLng pos2, DistanceUnit unit)
{
    double R = (unit == DistanceUnit.Miles) ? 3960 : 6371;
    var lat = (pos2.Latitude - pos1.Latitude).ToRadians();
    var lng = (pos2.Longitude - pos1.Longitude).ToRadians();
    var h1 = Math.Sin(lat / 2) * Math.Sin(lat / 2) +
                  Math.Cos(pos1.Latitude.ToRadians()) * Math.Cos(pos2.Latitude.ToRadians()) *
                  Math.Sin(lng / 2) * Math.Sin(lng / 2);
    var h2 = 2 * Math.Asin(Math.Min(1, Math.Sqrt(h1)));
    return R * h2;
}

下面是哈弗森公式的一个片段,找到了(
https://www.stormconsultancy.co.uk/blog/development/code-snippets/the-haversine-formula-in-c-and-sql/
)。看起来它有你需要的东西

public double HaversineDistance(LatLng pos1, LatLng pos2, DistanceUnit unit)
{
    double R = (unit == DistanceUnit.Miles) ? 3960 : 6371;
    var lat = (pos2.Latitude - pos1.Latitude).ToRadians();
    var lng = (pos2.Longitude - pos1.Longitude).ToRadians();
    var h1 = Math.Sin(lat / 2) * Math.Sin(lat / 2) +
                  Math.Cos(pos1.Latitude.ToRadians()) * Math.Cos(pos2.Latitude.ToRadians()) *
                  Math.Sin(lng / 2) * Math.Sin(lng / 2);
    var h2 = 2 * Math.Asin(Math.Min(1, Math.Sqrt(h1)));
    return R * h2;
}

这取决于你需要多准确的结果。我们的地球不是球体,而是椭球体(可以说半径在6378137m到6356752m之间)。这就是为什么为了得到准确的结果,你需要在非常高的水平上了解数学。如果您需要超过0.3%的精度,我建议您使用Charles Karney的GeographicalIB-


如果0.3%的精度足以使用@Mark代码。

这取决于您需要的结果的准确性。我们的地球不是球体,而是椭球体(可以说半径在6378137m到6356752m之间)。这就是为什么为了得到准确的结果,你需要在非常高的水平上了解数学。如果您需要超过0.3%的精度,我建议您使用Charles Karney的GeographicalIB-

如果0.3%级别的精度足以使用@Mark代码。

只需查看名称空间即可

var coord1 = new GeoCoordinate(lat1, lon1);
var coord2 = new GeoCoordinate(lat2, lon2);

var dist = coord1.GetDistanceTo(coord2);
有关更多信息:

现在,计算一组坐标到一个点的距离

List<GeoCoordinate> coordinates = ......;
GeoCoordinate point = ......;

var distances = coordinates.Select(c => c.GetDistanceTo(point))
                           .OrderBy()
                           .ToList();
列表坐标=。。。。。。;
地理坐标点=。。。。。。;
变量距离=坐标。选择(c=>c.GetDistanceTo(点))
.OrderBy()
.ToList();
只需查看名称空间即可

var coord1 = new GeoCoordinate(lat1, lon1);
var coord2 = new GeoCoordinate(lat2, lon2);

var dist = coord1.GetDistanceTo(coord2);
有关更多信息:

现在,计算一组坐标到一个点的距离

List<GeoCoordinate> coordinates = ......;
GeoCoordinate point = ......;

var distances = coordinates.Select(c => c.GetDistanceTo(point))
                           .OrderBy()
                           .ToList();
列表坐标=。。。。。。;
地理坐标点=。。。。。。;
变量距离=坐标。选择(c=>c.GetDistanceTo(点))
.OrderBy()
.ToList();

我认为,你可以在数学课上了解自己的需要。在这里,使用LINQ会帮你大忙。请将
LatLonClass
的定义添加到您的帖子中。我认为,您可以查看数学类以获得所需内容。使用LINQ将对您有很大帮助。请在您的帖子中添加
LatLonClass
的定义。