首页 > 解决方案 > 纬度/经度列表的中心位置

问题描述

我目前有一系列位置(带有纬度/经度),并且想知道如何为 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;}

}

标签: c#geospatialasp.net-core-3.1

解决方案


我使用列表中纬度和经度值的平均值来获取中心。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);
}

推荐阅读