首页 > 解决方案 > 查找给定半径内的所有点

问题描述

输入:给定特定坐标(经纬度)和距离,输出:显示该距离内的所有点(公里)

我怎样才能得到半径内的所有点我已经注释掉了我最初开始的代码,试图返回每个咖啡馆的最小距离,但不知道如何去做。

    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

      }

标签: c#.net

解决方案


输入:给定特定坐标(经纬度)和距离,输出:显示该距离内的所有点(公里)

我们没有该方法及其参数的所有上下文,但我假设该方法不接收空值并且它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以公里为单位。
这是一个显示计算两个坐标之间距离的不同方法的站点。

对于小距离,您甚至可以使用公式来计算平面中两点之间的距离。


推荐阅读