Пример #1
0
        /// <inheritdoc />
        public async Task <IEnumerable <LocationDto> > GetNearbyLocationsAsync(LocationDto location, int range,
                                                                               int maxResults = 0)
        {
            var query = from dbLoc in _repository
                        let radiansLat1 = Math.PI * dbLoc.Latitude / 180
                                          let radiansLat2                         = Math.PI * location.Latitude / 180
                                                                        let theta = dbLoc.Longitude - location.Longitude
                                                                                    let radiansTheta                       = Math.PI * theta / 180
                                                                                                                  let dist = Math.Acos(Math.Sin(radiansLat1) * Math.Sin(radiansLat2) +
                                                                                                                                       Math.Cos(radiansLat1) * Math.Cos(radiansLat2) * Math.Cos(radiansTheta)) *
                                                                                                                             180 * 60 * 1.1515 * 1609.344 / Math.PI // Radius of the earth ~6371km
                                                                                                                             where dist <= range
                                                                                                                             orderby dist
                                                                                                                             select new LocationDto {
                Latitude  = dbLoc.Latitude,
                Longitude = dbLoc.Longitude,
                Address   = dbLoc.Address,
                Distance  = dist
            };

            if (maxResults > 0)
            {
                query = query.Take(maxResults);
            }

            return(await query.ToListAsync());
        }
Пример #2
0
        /// <inheritdoc />
        public async Task <IEnumerable <LocationDto> > GetNearbyLocationsAsync(LocationDto location, int range, int maxResults)
        {
            var point = _geometryFactory.CreatePoint(new Coordinate(location.Longitude, location.Latitude));
            var query = from dbLoc in _repository
                        let distance = dbLoc.Coordinates.Distance(point)
                                       where distance <= range
                                       orderby distance
                                       select new LocationDto {
                Latitude  = dbLoc.Coordinates.Y,
                Longitude = dbLoc.Coordinates.X,
                Distance  = distance,
                Address   = dbLoc.Address
            };

            if (maxResults > 0)
            {
                query = query.Take(maxResults);
            }

            return(await query.ToListAsync());
        }