public virtual ViewResult Browse(int page = 1, double latitude = 53, double longitude = 8.8, string q = null, int r = 1) { var pageSize = 20; var currentPageIndex = page - 1; RavenQueryStatistics stats; var query = DocumentSession.Advanced.LuceneQuery<Thing, Thing_ByLocation>() .Statistics(out stats) .WaitForNonStaleResultsAsOfNow() .SortByDistance() .WithinRadiusOf(r, latitude: latitude, longitude: longitude); if (!string.IsNullOrWhiteSpace(q)) { query.Search(c => c.Name, q); } var items = query.Skip(currentPageIndex * pageSize) .Take(pageSize) .ToList(); var pos1 = new Position(latitude, longitude); foreach (var thing in items) { var pos2 = new Position(thing.Latitude, thing.Longitude); thing.DistanceFromYou = Haversine.Distance(pos1, pos2, DistanceType.Miles); } var list = items.ToPagedList(currentPageIndex, pageSize, stats.TotalResults); var model = new ThingBrowseModel { Radius = r, Query = q, Radii = new List<SelectListItem> { new SelectListItem() { Value = "1", Text = "1 miles", Selected = true }, new SelectListItem() { Value = "2", Text = "2 miles" }, new SelectListItem() { Value = "3", Text = "3 miles" }, new SelectListItem() { Value = "5", Text = "5 miles" }, new SelectListItem() { Value = "10", Text = "10 miles" }, new SelectListItem() { Value = "25", Text = "25 miles" }, }, Things = list }; return View("Browse", model); }
/// <summary> /// Returns the distance in miles or kilometers of any two /// latitude / longitude points. /// </summary> /// <param name="pos1">The pos1.</param> /// <param name="pos2">The pos2.</param> /// <param name="type">The type.</param> /// <returns></returns> public static double Distance(Position pos1, Position pos2,DistanceType type) { double r = (type == DistanceType.Miles) ? 3960 : 6371; var dLat = ToRadian(pos2.Latitude - pos1.Latitude); var dLon = ToRadian(pos2.Longitude - pos1.Longitude); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Cos(ToRadian(pos1.Latitude)) *Math.Cos(ToRadian(pos2.Latitude)) * Math.Sin(dLon / 2) * Math.Sin(dLon / 2); var c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a))); var d = r * c; return d; }