private IEnumerable <ResultModel> GetResults(Coordinate originCoordinate, double radius, DistanceUnit distanceUnit)
        {
            // Get the boundaries (min and max) latitude and longitude values. This forms a "square" around the origin coordinate
            // with each leg of the square exactly "X" miles from the origin, where X is the selected radius.
            var boundaries = new CoordinateBoundaries(originCoordinate.Latitude, originCoordinate.Longitude, radius, distanceUnit);

            // Select from all of the locations
            return(Locations
                   // Where the location's latitude is between the min and max latitude boundaries
                   .Where(x => x.Latitude >= boundaries.MinLatitude && x.Latitude <= boundaries.MaxLatitude)
                   // And where the location's longitude is between the min and max longitude boundaries
                   .Where(x => x.Longitude >= boundaries.MinLongitude && x.Longitude <= boundaries.MaxLongitude)
                   // Populate an instance of the Result class with the desired data, including distance/direction calculation
                   .Select(result => new ResultModel
            {
                Name = result.Name,
                Distance = GeoCalculator.GetDistance(originCoordinate.Latitude, originCoordinate.Longitude, result.Latitude, result.Longitude, distanceUnit: distanceUnit),
                Direction = GeoCalculator.GetDirection(originCoordinate.Latitude, originCoordinate.Longitude, result.Latitude, result.Longitude)
            })
                   // Filter by distance. This is necessary because a radius is a circle, yet we've defined a square around the origin coordinate.
                   // This filter removes any extraneous results that would appear in the square's "corners" (imagine placing a circle inside a square of the
                   // same size for visualization).
                   .Where(x => x.Distance <= radius)
                   // Sort by distance
                   .OrderBy(x => x.Distance));
        }
        public void GetDirectionThrowsArgumentExceptionWithInvalidDestinationCoordinates()
        {
            Coordinate origin      = Constants.Coordinates.ValidCoordinate;
            Coordinate destination = Constants.Coordinates.LatitudeBelowMinimum;

            var ex = Assert.Throws <ArgumentException>(() => GeoCalculator.GetDirection(origin.Latitude, origin.Longitude, destination.Latitude, destination.Longitude));

            Assert.AreEqual("Invalid destination coordinates supplied.", ex.Message);
        }
        public void GetDirectionWithCoordinateObjectReturnsCorrectNorthWestResult()
        {
            Coordinate origin      = Constants.Coordinates.ValidCoordinate;
            Coordinate destination = Constants.Coordinates.ValidDestinationCoordinate;

            string       direction      = GeoCalculator.GetDirection(origin, destination);
            const string expectedResult = "NW";

            Assert.AreEqual(expectedResult, direction);
        }
        public void GetDirectionReturnsCorrectNorthWestResult()
        {
            Coordinate origin      = Constants.Coordinates.ValidCoordinate;
            Coordinate destination = Constants.Coordinates.ValidDestinationCoordinate;

            string       direction      = GeoCalculator.GetDirection(origin.Latitude, origin.Longitude, destination.Latitude, destination.Longitude);
            const string expectedResult = "NW";

            Assert.AreEqual(direction, expectedResult);
        }
示例#5
0
        public async Task <ActionResult> GetBusinessforCustomer(BusinessForClientViewmodel businessForClientViewmodel)
        {
            var totalFeedBack = 0.0;
            var response      = await context.Businesses.Include(bt => bt.businessType).Include(f => f.FeedBacks).Where(iv => iv.isVisible).AsNoTracking().ToListAsync();

            for (int i = 0; i < response.Count; i++)
            {
                if (response[i].FeedBacks.Count > 0)
                {
                    for (int j = 0; j < response[i].FeedBacks.Count; j++)
                    {
                        if (response[i].FeedBacks[j].OverallRating.HasValue)
                        {
                            totalFeedBack += response[i].FeedBacks[j].OverallRating.Value;
                        }
                    }
                    response[i].OverallRating = totalFeedBack / double.Parse(response[i].FeedBacks.Count.ToString());
                }
            }
            Coordinate           origin     = new Coordinate(businessForClientViewmodel.UserLatitude, businessForClientViewmodel.UserLongitude);
            CoordinateBoundaries boundaries = new CoordinateBoundaries(origin, 10);

            double minLatitude  = boundaries.MinLatitude;
            double maxLatitude  = boundaries.MaxLatitude;
            double minLongitude = boundaries.MinLongitude;
            double maxLongitude = boundaries.MaxLongitude;

            var results = response
                          .Where(x => x.Latitude >= minLatitude && x.Latitude <= maxLatitude)
                          .Where(x => x.Longitude >= minLongitude && x.Longitude <= maxLongitude)
                          .Select(result => new
            {
                Store     = result,
                Distance  = GeoCalculator.GetDistance(origin.Latitude, origin.Longitude, result.Latitude, result.Longitude, 1),
                Direction = GeoCalculator.GetDirection(origin.Latitude, origin.Longitude, result.Latitude, result.Longitude)
            })
                          .Where(x => x.Distance <= 10)
                          .OrderBy(x => x.Distance)
                          .ToList();

            response = results.Select(x => x.Store).ToList();
            return(new OkObjectResult(response));
        }
示例#6
0
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            double radius = Convert.ToDouble(Radius.SelectedValue);

            // These coordinates would normally be retrieved from a geocoding API such as Google's or Bing's.
            // See https://github.com/scottschluer/Geocoder for an easy to use Geocoder API for Google.
            Coordinate originCoordinate = new Coordinate {
                Latitude = 34.076234, Longitude = -118.395314
            };

            // Get the boundaries (min and max) latitude and longitude values. This forms a "square" around the origin coordinate
            // with each leg of the square exactly "X" miles from the origin, where X is the selected radius.
            CoordinateBoundaries boundaries = new CoordinateBoundaries(originCoordinate.Latitude, originCoordinate.Longitude, radius);

            // Select from all of the locations
            IEnumerable <Result> results = _locations
                                           // Where the location's latitude is between the min and max latitude boundaries
                                           .Where(x => x.Latitude >= boundaries.MinLatitude && x.Latitude <= boundaries.MaxLatitude)
                                           // And where the location's longitude is between the min and max longitude boundaries
                                           .Where(x => x.Longitude >= boundaries.MinLongitude && x.Longitude <= boundaries.MaxLongitude)
                                           // Populate an instance of the Result class with the desired data, including distance/direction calculation
                                           .Select(result => new Result
            {
                Name      = result.Name,
                Distance  = GeoCalculator.GetDistance(originCoordinate.Latitude, originCoordinate.Longitude, result.Latitude, result.Longitude, 1),
                Direction = GeoCalculator.GetDirection(originCoordinate.Latitude, originCoordinate.Longitude, result.Latitude, result.Longitude)
            })
                                           // Filter by distance. This is necessary because a radius is a circle, yet we've defined a square around the origin coordinate.
                                           // This filter removes any extraneous results that would appear in the square's "corners" (imagine placing a circle inside a square of the
                                           // same size for visualization).
                                           .Where(x => x.Distance <= radius)
                                           // Sort by distance
                                           .OrderBy(x => x.Distance);

            gvLocations.DataSource = results;
            gvLocations.DataBind();
        }