纬度/经度列表的中心位置

问题描述

我目前有一系列位置(含经/纬度),并且想知道如何为C#列表中的所有位置查找大致的中心位置。

我发现的示例使用不同的编程语言。我将发布到目前为止的工作成果,但我只是停留在开始的地方。

谢谢

public (double lat,double lon) GetCenterLocation(Location[] locs)
{

}


public class Location {

    public long Id {get;set;}

    public double Latitude {get;set;}

    public double Longitude {get;set;}

}

解决方法

我使用列表中纬度和经度的平均值来获取中心。 53.409533,0.974654至少可以让我得到一些回馈。

public static (double Latitude,double Longitude) GetCenter(this List<LatLngPoint> points)
{
    if (points == null || points.Count == 0)
        return (53.409533,0.974654);

    var lat = points.Where(ll => ll.Latitude.HasValue).Average(ll => ll.Latitude.Value);
    var lon = points.Where(ll => ll.Longitude.HasValue).Average(ll => ll.Longitude.Value);

    return (lat,lon);
}