public SearchParameters()
 {
     Facets = new Dictionary<string, string>();
       //  FacetDates = new Dictionary<string, string>();
       //  FacetQueries = new Dictionary<string, string>();
     UserLoc = new GLatLong();
     PageSize = DefaultPageSize;
     PageIndex = 1;
     UserID = -1;
 }
Exemplo n.º 2
0
 public SearchParameters()
 {
     Facets = new Dictionary <string, string>();
     //  FacetDates = new Dictionary<string, string>();
     //  FacetQueries = new Dictionary<string, string>();
     UserLoc   = new GLatLong();
     PageSize  = DefaultPageSize;
     PageIndex = 1;
     UserID    = -1;
 }
        public static string GetAddress(GLatLong latlong)
        {
            WebRequest request = WebRequest
               .Create("http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address=" + latlong);

            using (WebResponse response = request.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    XDocument document = XDocument.Load(new StreamReader(stream));

                    string address = document.Descendants("formatted_address").FirstOrDefault().ToString();
                   return address;
                }
            }
        }
        private GLatLong GetUserLocation(int UserId)
        {
            var userdetails = dbmeals.UserDetails.FirstOrDefault(u => u.UserId == UserId);
            GLatLong userloc = new GLatLong();
            if (userdetails != null)
            {
                userloc.Latitude = Convert.ToDouble(userdetails.AddressList.Latitude);
                userloc.Longitude = Convert.ToDouble(userdetails.AddressList.Longitude);

                return userloc;
            }

            else
                return null;
        }
        public GLatLong GetLatLng(string address)
        {
            //IGeoCoder geoCoder = new GoogleGeoCoder("my-api-key");
            //Address[] addresses = geoCoder.GeoCode("123 Main St");

            //IGeoCoder geoCoder = new YahooGeoCoder("my-app-ID");
            // addresses = geoCoder.GeoCode(38.8976777, -77.036517);

            GLatLong loc = new GLatLong();
            var c = GeoCodingHelper.GetLatLong(address);
            if (c != null)
            {
                loc.Latitude = c.Latitude;
                loc.Longitude = c.Longitude;
            }
            return loc;
        }
        public ActionResult LocationToSearch(string address)
        {
            if (!string.IsNullOrEmpty(address))
            {

                GLatLong loc = new GLatLong();
                //    loc = GeoCodingHelper.GetLatLong(address);//uncomment this

                loc.Latitude = 41.330215; //comment this
                loc.Longitude = -73.859004;//comment this

                Session["UserLocLat"] = loc.Latitude;
                Session["UserLocLong"] = loc.Longitude;
                Session["UserLoc"] = address;

                LocationsSearched ls = new LocationsSearched();
                ls.Location = address;
                ls.Latitude = Convert.ToDecimal(loc.Latitude);
                ls.Longitude = Convert.ToDecimal(loc.Longitude);
                ls.DateCreated = DateTime.Now;
                ls.UserID = WebSecurity.CurrentUserId;
                dbmeals.LocationsSearcheds.Add(ls);
                dbmeals.SaveChanges();

                SearchParam searchparam = new SearchParam();
                return RedirectToAction("Index", "Home", new RouteValueDictionary(searchparam));

            }

            return View();
        }
        public ActionResult Index(SearchParam parameters)
        {
            if (TempData["ErrorMessage"] != null)
            {
                ViewBag.ErrorMessage = TempData["ErrorMessage"];
            }
            else
            {
                ViewBag.ErrorMessage = "";
            }
            var ChangeLocation = HttpContext.Request.QueryString["ChangeLocation"];

            if ((ChangeLocation != null) && (ChangeLocation != ""))
            {

                GLatLong loc = new GLatLong();
                loc = GeoCodingHelper.GetLatLong(ChangeLocation.ToString());

                if (loc != null)
                {
                    Session["UserLocLat"] = loc.Latitude;
                    Session["UserLocLong"] = loc.Longitude;
                }
                Session["UserLoc"] = ChangeLocation;
            }
            else
            {
                var LocationsSearched = dbmeals.LocationsSearcheds.Where(x => x.UserID == WebSecurity.CurrentUserId).OrderByDescending(x => x.DateCreated).ToList().FirstOrDefault();
                if (LocationsSearched != null)
                {
                    Session["UserLoc"] = LocationsSearched.Location;
                    parameters.FreeSearch = LocationsSearched.Keywords;
                    Session["UserLocLat"] = LocationsSearched.Latitude;
                    Session["UserLocLong"] = LocationsSearched.Longitude;
                    parameters.DistanceSearch = LocationsSearched.Distance.HasValue ? LocationsSearched.Distance.Value.ToString() : string.Empty;
                    parameters.PickUpDateSearch = Common.AbsoluteEnd(LocationsSearched.DateRangeStart.HasValue ? LocationsSearched.DateRangeStart.Value : DateTime.Now);// LocationsSearched.DateRangeStart;
                    //parameters.DateRangeEnd = LocationsSearched.DateRangeEnd;
                }
            }

            if (String.IsNullOrEmpty(parameters.DistanceSearch))
                parameters.DistanceSearch = "100";

            if (parameters.PickUpDateSearch == null)
                parameters.PickUpDateSearch = DateTime.Now;

            string pointofreference = Session["UserLocLat"] + "," + Session["UserLocLong"];

            LocationsSearched ls = new LocationsSearched();
            ls.Location = Convert.ToString(Session["UserLoc"]);//.ToString();
            ls.Keywords = parameters.FreeSearch;
            ls.Latitude = Convert.ToDecimal(Session["UserLocLat"]);
            ls.Longitude = Convert.ToDecimal(Session["UserLocLong"]);
            ls.DateRangeStart = Common.AbsoluteStart(parameters.PickUpDateSearch);
            ls.DateRangeEnd = Common.AbsoluteEnd(parameters.PickUpDateSearch);
            ls.Distance = Convert.ToInt32(parameters.DistanceSearch);
            ls.DistanceUnit = "km";
            ls.DateCreated = DateTime.Now;
            ls.UserID = WebSecurity.CurrentUserId;
            try
            {
                dbmeals.LocationsSearcheds.Add(ls);
                dbmeals.SaveChanges();
            }
            catch (Exception e)
            {

                string message = e.Message;
            }
            try
            {

                ICollection<ISolrFacetQuery> list =
                AllFacetFields.Except(SelectedFacetFields(parameters)).
            Select(f => new SolrFacetFieldQuery(f) { MinCount = 1 })
            .Cast<ISolrFacetQuery>().ToList();

                var start = (parameters.PageIndex - 1) * parameters.PageSize;

                var matchingProducts = solr.Query(BuildQuery(parameters), new QueryOptions
                {
                    FilterQueries = BuildFilterQueries(parameters),
                    Rows = parameters.PageSize,
                    Start = start,
                    OrderBy = GetSelectedSort(parameters),
                    SpellCheck = new SpellCheckingParameters(),
                    Facet = new FacetParameters
                    {
                        Queries = list

                    },

                    ExtraParams = new Dictionary<string, string>
                                          {
                                              // uncomment for filtering by distance
                                              {"fq", "{!geofilt}"},
                                              {"d", parameters.DistanceSearch},// distance.ToString(CultureInfo.InvariantCulture)} replace distance with your radius filter
                                              {"sfield", "latlng"}, // replace lat_long with your field in solr that stores the lat long values
                                              {"pt", pointofreference},// this is the point of reference
                                             // {"sort","geodist() asc"},
                                               {"fl","*,Distance:geodist()"},

                                          }

                });

                var distancemodel = new DistanceViewModel();

                // var matchingProducts

                var ProductModel = new ProductView
                {
                    WholeSet = matchingProducts,
                    Search = parameters,
                    TotalCount = matchingProducts.NumFound,
                    Facets = matchingProducts.FacetFields,
                    //  PickUpTimeFacet = GetPickUpTimeFacet(matchingProducts.FacetQueries),
                    // DistanceFacet = GetDistanceFacet(matchingProducts.FacetQueries),
                    DidYouMean = GetSpellCheckingResult(matchingProducts),
                    //DistanceLimitList.SelectedDistance = parameters.DistanceLimit

                };

                // Preselect the option with Value = "US"
                // Make sure you have such option in the Countries list

                List<SolrResultSet> SolrResultSetList = new List<SolrResultSet>();
                SolrResultSetList = (from n in ProductModel.WholeSet
                                     select n).ToList();

                var ProductViewModel = new ResultSetViewModel();
                ProductViewModel.Search = ProductModel.Search;
                ProductViewModel.TotalCount = ProductModel.TotalCount;
                ProductViewModel.Facets = ProductModel.Facets;
                ProductViewModel.DidYouMean = ProductModel.DidYouMean;
                ProductViewModel.ProviderList = (from n in SolrResultSetList
                                                 group n by new
                                                 {
                                                     n.ProviderName,
                                                     n.ProviderType
                                                     ,
                                                     n.Distance,
                                                     n.latlng,
                                                     n.PhoneNumber,
                                                     n.FullAddress,
                                                     n.Cuisine
                                                 } into g
                                                 select new Provider()
                                                 {
                                                     ProviderName = g.Key.ProviderName,
                                                     ProviderType = g.Key.ProviderType,
                                                     Distance = g.Key.Distance,
                                                     latlng = g.Key.latlng,
                                                     PhoneNumber = g.Key.PhoneNumber,
                                                     FullAddress = g.Key.FullAddress,
                                                     Cuisine = g.Key.Cuisine,
                                                 }).ToList();

                foreach (Provider p in ProductViewModel.ProviderList)
                {

                    p.Products = (from n in SolrResultSetList
                                  where n.ProviderName == p.ProviderName
                                  select new Product()

                                      {
                                          MealAdID = n.MealAdID,
                                          MealItemName = n.MealItemName,
                                          FoodType = n.FoodType,
                                          MealType = n.MealType,
                                          Price = n.Price,
                                          Ingredients = n.Ingredients,
                                          AllergenicIngredients = n.AllergenicIngredients,
                                          Timestamp = n.Timestamp,
                                          Description = n.Description

                                      }).ToList();
                }

                distancemodel.SelectedDistanceLimit = parameters.DistanceSearch;
                ProductViewModel.DistanceDD = distancemodel;

                return View(ProductViewModel);

            }
            catch (SolrConnectionException e)
            {

                string msg = e.Message;
                return View(new ResultSetViewModel
                {
                    QueryError = true,
                });

            }
        }
        public ActionResult LocateAddress(string address)
        {
            //address = "1 whitehall street new york ny 10004";

            //IGeoCoder geoCoder = new GoogleGeoCoder("my-api-key");
            //Address[] addresses = geoCoder.GeoCode("123 Main St");

            //IGeoCoder geoCoder = new YahooGeoCoder("my-app-ID");
            // addresses = geoCoder.GeoCode(38.8976777, -77.036517);

            GLatLong loc = new GLatLong();
            var c = GeoCodingHelper.GetLatLong(address);

            loc.Latitude = c.Latitude;
            loc.Longitude = c.Longitude;

            string revgeocode = "";

            var g = GeoCodingHelper.GetAddress(loc);
            revgeocode = g.ToString();

            return View(loc);
        }