public JsonResult GetEnterprisesCloseToMyLocation(string latitude, string longitude) { var enterprises = Db.Enterprises.GetNearbyEnterprises(latitude, longitude); var searchViewModel = new MainSearchViewModel { Enterprises = new List <LightEnterprise>() }; foreach (var enterpriseViewModel in enterprises.Select(enterprise => new LightEnterprise { Key = EnterpriseHelper.GetKey(enterprise.Id), Name = enterprise.Name, LocationInfo = EnterpriseHelper.FormatDisplayStreet(enterprise), DistanceFromMyLocation = string.Empty, Categories = EnterpriseHelper.GetDisplayLabelsCategories(enterprise.Categories), Coordinates = enterprise.Coordinates, })) { searchViewModel.Enterprises.Add(enterpriseViewModel); } var myCoord = new GeoCoordinate(double.Parse(latitude, CultureInfo.InvariantCulture), double.Parse(longitude, CultureInfo.InvariantCulture)); foreach (var enterprise in searchViewModel.Enterprises) { var enterpriseCoord = new GeoCoordinate(enterprise.Coordinates.Lat, enterprise.Coordinates.Lng); var distance = myCoord.GetDistanceTo(enterpriseCoord); enterprise.DistanceFromMyLocation = string.Format("{0}km", Math.Round((distance / 1000), 1)); } return(Json(searchViewModel)); }
public JsonResult MainSearch(string searchTerm) { if (string.IsNullOrEmpty(searchTerm)) { return(Json(null)); } var enterprises = Db.Enterprises.MainSearch(searchTerm); var searchViewModel = new MainSearchViewModel { SearchQuery = searchTerm, Enterprises = new List <LightEnterprise>(), Locations = new List <string> { "Tumba", "Tullinge" }, Categories = new List <string>() }; var categories = GeneralHelper.GetCategories().Where(c => c.Text.ToLower().StartsWith(searchTerm.ToLower())); if (categories.Any()) { searchViewModel.Categories = categories.Select(c => c.Text).ToList(); } foreach (var enterpriseViewModel in enterprises.Select(enterprise => new LightEnterprise { Key = EnterpriseHelper.GetKey(enterprise.Id), Name = enterprise.Name, LocationInfo = EnterpriseHelper.FormatDisplayStreet(enterprise), DistanceFromMyLocation = string.Empty, Categories = EnterpriseHelper.GetDisplayLabelsCategories(enterprise.Categories) })) { searchViewModel.Enterprises.Add(enterpriseViewModel); } return(Json(searchViewModel)); }