// // GET: /WhereToBuy/ public ActionResult Index() { string dealersearch = Request.QueryString["search"]; ViewBag.dealersearch = dealersearch; if (dealersearch != null && dealersearch.Trim() != "") { List<DealerLocation> localdealers = DealerModel.SearchLocationsByType(dealersearch); int loccount = localdealers.Count; if (loccount == 0) { // Query Google Maps API for a geolocation LatLong location = new LatLong(); try { string apiurl = "http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address=" + dealersearch; XDocument xdoc = XDocument.Load(apiurl); location = (from latlng in xdoc.Descendants("result").Descendants("geometry").Descendants("location") select new LatLong { latitude = Convert.ToDouble(latlng.Element("lat").Value), longitude = Convert.ToDouble(latlng.Element("lng").Value) }).First<LatLong>(); } catch { }; if (location.latitude != null && location.longitude != null) { localdealers = DealerModel.SearchLocationsByLatLong(location); } } ViewBag.localdealers = localdealers; } List<Customer> onlinedealers = DealerModel.GetEtailers(); ViewBag.onlinedealers = onlinedealers; List<MapGraphics> localtiers = DealerModel.GetLocalDealerTypes(); ViewBag.localtiers = localtiers; return View(); }
public static List<DealerLocation> SearchLocationsByLatLong(LatLong location) { // Use Haversine formula to find nearest dealers within 100 miles CurtDevDataContext db = new CurtDevDataContext(); double earth = 3963.1676; // radius of Earth in miles double lat1 = location.latitude; double lon1 = location.longitude; List<DealerLocation> locations = (from cl in db.CustomerLocations join c in db.Customers on cl.cust_id equals c.cust_id join dt in db.DealerTypes on c.dealer_type equals dt.dealer_type join dtr in db.DealerTiers on c.tier equals dtr.ID where dt.online == false && dt.show == true && c.isDummy == false && (earth * ( 2 * Math.Atan2( Math.Sqrt((Math.Sin(((cl.latitude - lat1) * (Math.PI / 180)) / 2) * Math.Sin(((cl.latitude - lat1) * (Math.PI / 180)) / 2)) + ((Math.Sin(((cl.longitude - lon1) * (Math.PI / 180)) / 2)) * (Math.Sin(((cl.longitude - lon1) * (Math.PI / 180)) / 2))) * Math.Cos(location.latitudeRadians) * Math.Cos(cl.latitude * (Math.PI / 180))), Math.Sqrt(1 - ((Math.Sin(((cl.latitude - lat1) * (Math.PI / 180)) / 2) * Math.Sin(((cl.latitude - lat1) * (Math.PI / 180)) / 2)) + ((Math.Sin(((cl.longitude - lon1) * (Math.PI / 180)) / 2)) * (Math.Sin(((cl.longitude - lon1) * (Math.PI / 180)) / 2))) * Math.Cos(location.latitudeRadians) * Math.Cos(cl.latitude * (Math.PI / 180)))) ) ) < 100.0) select new DealerLocation { distance = earth * ( 2 * Math.Atan2( Math.Sqrt((Math.Sin(((cl.latitude - lat1) * (Math.PI / 180)) / 2) * Math.Sin(((cl.latitude - lat1) * (Math.PI / 180)) / 2)) + ((Math.Sin(((cl.longitude - lon1) * (Math.PI / 180)) / 2)) * (Math.Sin(((cl.longitude - lon1) * (Math.PI / 180)) / 2))) * Math.Cos(location.latitudeRadians) * Math.Cos(cl.latitude * (Math.PI / 180))), Math.Sqrt(1 - ((Math.Sin(((cl.latitude - lat1) * (Math.PI / 180)) / 2) * Math.Sin(((cl.latitude - lat1) * (Math.PI / 180)) / 2)) + ((Math.Sin(((cl.longitude - lon1) * (Math.PI / 180)) / 2)) * (Math.Sin(((cl.longitude - lon1) * (Math.PI / 180)) / 2))) * Math.Cos(location.latitudeRadians) * Math.Cos(cl.latitude * (Math.PI / 180)))) ) ), State = cl.State, dealerType = dt, dealerTier = dtr, locationID = cl.locationID, name = cl.name, address = cl.address, city = cl.city, stateID = cl.stateID, postalCode = cl.postalCode, email = cl.email, phone = cl.phone, fax = cl.fax, latitude = cl.latitude, longitude = cl.longitude, cust_id = cl.cust_id, isprimary = cl.isprimary, contact_person = cl.contact_person, websiteurl = (c.eLocalURL == null || c.eLocalURL.Trim() == "") ? ((c.website == null || c.website.Trim() == "") ? "" : c.website) : c.eLocalURL, showWebsite = c.showWebsite }).OrderBy(x => x.distance).ToList<DealerLocation>(); return locations; }