c# - 纬度/经度列表的中心位置
问题描述
我目前有一系列位置(带有纬度/经度),并且想知道如何为 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);
}
推荐阅读
- c# - 三元运算符的用法没有错误的情况
- flutter - 用倒色进行颤振绘制
- scala - 在 Scala 中序列化函数时避免捕获 `this`
- c++ - [[maybe_unused]] 与结构化绑定?
- c# - gRPC C#服务器端,等到客户端关闭连接?
- xamarin.forms - Xamarin Forms 4.4 + 中是否仍需要此 ExportRenderer
- html - 如何在 .ts 文件中以角度创建按钮并添加点击事件
- javascript - 正则表达式 - 在最少两个字符之后
- python - 如何使用 API 获取 Reddit 提交的评论?
- windows - 识别文件是否未写入的程序