public static void GetStores()
        {
            double latitude = 37.52411;
            double longitude = -122.25914;

            var loc = new GeoLocation(latitude, longitude);

            int offeringId = 2;
            var context = new IPTV2Entities();
            var stores = StoreFront.GetNearestStores(context, offeringId, loc, false);

        }
示例#2
0
        public static double Distance(GeoLocation loc1, GeoLocation loc2, bool inKilometers)
        {
            /*
            The Haversine formula according to Dr. Math.
            http://mathforum.org/library/drmath/view/51879.html
                
            dlon = lon2 - lon1
            dlat = lat2 - lat1
            a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2
            c = 2 * atan2(sqrt(a), sqrt(1-a)) 
            d = R * c
                
            Where
                * dlon is the change in longitude
                * dlat is the change in latitude
                * c is the great circle distance in Radians.
                * R is the radius of a spherical Earth.
                * The locations of the two points in 
                    spherical coordinates (longitude and 
                    latitude) are lon1,lat1 and lon2, lat2.
            */

            double dDistance = Double.MinValue;
            double dLat1InRad = loc1.Latitude * (Math.PI / 180.0);
            double dLong1InRad = loc1.Longitude * (Math.PI / 180.0);
            double dLat2InRad = loc2.Latitude * (Math.PI / 180.0);
            double dLong2InRad = loc2.Longitude * (Math.PI / 180.0);

            double dLongitude = dLong2InRad - dLong1InRad;
            double dLatitude = dLat2InRad - dLat1InRad;

            // Intermediate result a.
            double a = Math.Pow(Math.Sin(dLatitude / 2.0), 2.0) +
                       Math.Cos(dLat1InRad) * Math.Cos(dLat2InRad) *
                       Math.Pow(Math.Sin(dLongitude / 2.0), 2.0);

            // Intermediate result c (great circle distance in Radians).
            double c = 2.0 * Math.Asin(Math.Sqrt(a));

            dDistance = c * (inKilometers ? EarthRadiusInKms : EarthRadiusInMiles);

            return dDistance;
        }
示例#3
0
 /// <summary>
 /// GetNearestStores to a specific location, order by distance
 /// </summary>
 /// <param name="context">Database Context</param>
 /// <param name="offeringId">Offering Id</param>
 /// <param name="loc">Origin Location</param>
 /// <param name="inKilometers">In Kilometers or Miles</param>
 /// <param name="maximumDistance">Maximum distance from origin</param>
 /// <returns></returns>
 public static SortedSet<StoreFrontDistance> GetNearestStores(IPTV2Entities context, int offeringId, GeoLocation loc, bool inKilometers, double maximumDistance)
 {
     var stores = new SortedSet<StoreFrontDistance>();
     var allStores = context.StoreFronts.Where(sf => sf.OfferingId == offeringId && sf.StatusId == 1);
     foreach (var s in allStores)
     {
         try
         {
             double distance = GeoLocation.Distance(loc, new GeoLocation((double)s.Latitude, (double)s.Longitude), inKilometers);
             if (distance <= maximumDistance)
             {
                 stores.Add(new StoreFrontDistance(distance, s));
             }
         }
         catch (Exception)
         {
         }
     }
     return stores;
 }
        public ActionResult Index()
        {
            try
            {
                var context = new IPTV2Entities();
                string userIp = Request.GetUserHostAddressFromCloudflare();
                if (Request.IsLocal)
                    userIp = "219.101.156.70";
                if (GlobalConfig.isUAT)
                {
                    if (!String.IsNullOrEmpty(Request["ip"]))
                        userIp = Request["ip"];
                }

                var ipLocation = MyUtility.getLocation(userIp);
                GeoLocation location = new GeoLocation() { Latitude = ipLocation.latitude, Longitude = ipLocation.longitude };
                SortedSet<StoreFrontDistance> result;
                if (GlobalConfig.maximumDistance != 0)
                    result = StoreFront.GetNearestStores(context, GlobalConfig.offeringId, location, true, GlobalConfig.maximumDistance);
                else
                    result = StoreFront.GetNearestStores(context, GlobalConfig.offeringId, location, true);

                List<StoreFront> stores = new List<StoreFront>();
                if (result != null)
                {
                    foreach (var item in result)
                        stores.Add(item.Store);
                    //var result = context.StoreFronts.Where(s => s.StatusId == GlobalConfig.Visible).ToList();
                    ViewBag.Location = ipLocation;
                    if (!Request.Cookies.AllKeys.Contains("version"))
                        return View("Index2", stores);
                    return View(stores);
                }
            }
            catch (Exception e) { MyUtility.LogException(e); }
            return RedirectToAction("Index", "Home");
        }
示例#5
0
        public static string GetDealerNearUser(IPTV2Entities context, User user, int offeringId)
        {
            string dealers = String.Empty;
            try
            {
                string userIp = user.RegistrationIp;
                if (!String.IsNullOrEmpty(userIp))
                {
                    var ipLocation = MyUtility.getLocation(userIp);
                    GeoLocation location = new GeoLocation() { Latitude = ipLocation.latitude, Longitude = ipLocation.longitude };
                    SortedSet<StoreFrontDistance> result;
                    if (Convert.ToInt32(Settings.GetSettings("maximumDistance")) != 0)
                        result = StoreFront.GetNearestStores(context, offeringId, location, true, 1000);
                    else
                        result = StoreFront.GetNearestStores(context, offeringId, location, true);
                    StringBuilder sb = new StringBuilder();
                    var ctr = 0;
                    foreach (var item in result)
                    {
                        var email = item.Store.EMailAddress;
                        var fullAddress = String.Format("{0}, {1}, {2} {3}", item.Store.Address1, item.Store.City, item.Store.State, item.Store.ZipCode);
                        string li = String.Format("<li>{0}<br />Address: {1}<br />Phone: {2}</li>", item.Store.BusinessName, fullAddress, item.Store.BusinessPhone);
                        sb.AppendLine(li);
                        ctr++;
                        if (ctr + 1 > 3)
                            break;
                    }

                    dealers = sb.ToString();
                }
            }
            catch (Exception e) { Trace.WriteLine(e.Message); }
            return dealers;
        }
示例#6
0
 public static double DistanceInMiles(GeoLocation loc1, GeoLocation loc2)
 {
     return Distance(loc1, loc2, false);
 }
示例#7
0
 public static double DistanceInKilometers(GeoLocation loc1, GeoLocation loc2)
 {
     return Distance(loc1, loc2, true);
 }
示例#8
0
 public static SortedSet<StoreFrontDistance> GetNearestStores(IPTV2Entities context, int offeringId, GeoLocation loc, bool inKilometers)
 {
     return GetNearestStores(context, offeringId, loc, inKilometers, double.MaxValue);
 }
示例#9
0
 public static SortedSet<StoreFrontDistance> GetNearestStoresInMiles(IPTV2Entities context, int offeringId, GeoLocation loc, double maximumDistance)
 {
     return GetNearestStores(context, offeringId, loc, false, maximumDistance);
 }
示例#10
0
 public static SortedSet<StoreFrontDistance> GetNearestStoresInKilometers(IPTV2Entities context, int offeringId, GeoLocation loc)
 {
     return GetNearestStores(context, offeringId, loc, true);
 }