c# - 查找给定半径内的所有点
问题描述
输入:给定特定坐标(经纬度)和距离,输出:显示该距离内的所有点(公里)
我怎样才能得到半径内的所有点我已经注释掉了我最初开始的代码,试图返回每个咖啡馆的最小距离,但不知道如何去做。
public async Task<PaginationResponse> GetSearchVenue(int? MaxDistance, double? latitude, double? longitude, int pageNumber, int pageCount, bool active)
{
var cafe = _context.Restaurant.Where(w => w.IsDeleted != active);
Point currentLocation = null;
if (latitude.HasValue && longitude.HasValue)
{
currentLocation = new Point(latitude.Value, longitude.Value)
{
SRID = 4326
};
cafe = cafe.Where(w => w.Latitude.HasValue && w.Longitude.HasValue);
}
//calculate maxdistance
var distanceInKm = currentLocation.Distance(new Point(latitude.Value, longitude.Value)) / 1000;
//if its less than the maxdistance then display all the restaurants in that radius
//if(distanceInKm < MaxDistance)
//{
// cafe = cafe.Where(w => w.);
//}
//gets the closest distance to current location
Distance = currentLocation != null ? currentLocation.Distance(new Point(s.Latitude.Value, s.Longitude.Value)) : 0
}
解决方案
输入:给定特定坐标(经纬度)和距离,输出:显示该距离内的所有点(公里)
我们没有该方法及其参数的所有上下文,但我假设该方法不接收空值并且它cafe
是经纬度的 IEnumerable,下面的代码应该可以工作。
void SearchNearby (double max_distance, double latitude, double longitude) {
return cafe.Where((lat, lon) => {
return CalcDistance(lat,lon, latitude, longitude) <= max_distance;
});
}
当然也假设max_distance
以公里为单位。
这是一个显示计算两个坐标之间距离的不同方法的站点。
对于小距离,您甚至可以使用公式来计算平面中两点之间的距离。
推荐阅读
- c# - WaitOne 什么时候执行非框架代码?
- android - Oreo:startService() 在后台调用时不会抛出 IllegalStateException
- javascript - 选项标签中的动态选项在JavaScript中使用带有html标签的'For循环'?
- c++ - 在 C++ 中,创建“n”个对象的推荐方法是什么,其中 n 是用户定义的。我怎样才能做到这一点?
- r - 如何在 bookdown::gitbook 中使用自定义 pandoc HTML 模板
- html - 图像不在页面中间水平居中
- firebase - 火力基地 | 使用 Firebase 控制台以 CSV 格式导出收集数据
- javascript - 我应该如何解析 Cheerio 中的复杂元素
- javascript - JS/PHP:在 Outlook 中撰写电子邮件并添加附件
- google-cloud-platform - 谷歌计算引擎,附加磁盘不工作