public static LatLng GetLatLngStatic(string addr, string city, int stateID, string key) { CurtDevDataContext db = new CurtDevDataContext(); // Get the state abbreviation that matches the stateID string state_abbr = (from ps in db.PartStates where ps.stateID.Equals(stateID) select ps.abbr).FirstOrDefault<string>(); string url = "http://maps.google.com/maps/geo?output=csv&key=" + key + "&q=" + HttpContext.Current.Server.UrlEncode(addr + " " + city + ", " + state_abbr); var request = WebRequest.Create(url); var response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { var ms = new MemoryStream(); var responseStream = response.GetResponseStream(); var buffer = new Byte[2048]; int count = responseStream.Read(buffer, 0, buffer.Length); while (count > 0) { ms.Write(buffer, 0, count); count = responseStream.Read(buffer, 0, buffer.Length); } responseStream.Close(); ms.Close(); var responseBytes = ms.ToArray(); var encoding = new System.Text.ASCIIEncoding(); var coords = encoding.GetString(responseBytes); var parts = coords.Split(','); LatLng loc = new LatLng { latitude = parts[2], longitude = parts[3] }; return loc; } return null; }
public LatLng GetLatLng(string addr, string city, int stateID) { CurtDevDataContext db = new CurtDevDataContext(); // Get the state abbreviation that matches the stateID string state_abbr = (from ps in db.PartStates where ps.stateID.Equals(stateID) select ps.abbr).FirstOrDefault<string>(); string url = "http://maps.google.com/maps/geo?output=csv&key=" + this.API_KEY + "&q=" + HttpContext.Current.Server.UrlEncode(addr + " " + city + ", " + state_abbr); WebClient wc = new WebClient(); wc.Proxy = null; string coords = wc.DownloadString(url); if (coords != null && coords != "") { var parts = coords.Split(','); LatLng loc = new LatLng { latitude = parts[2], longitude = parts[3] }; return loc; } return null; }
public ActionResult Add() { string error = ""; #region Form Submission try { if (Request.Form["btnSubmit"] != null) { CurtDevDataContext db = new CurtDevDataContext(); // Save form values string name = (Request.Form["name"] != null) ? Request.Form["name"] : ""; string email = (Request.Form["email"] != null) ? Request.Form["email"] : ""; string phone = (Request.Form["phone"] != null) ? Request.Form["phone"] : ""; string fax = (Request.Form["fax"] != null) ? Request.Form["fax"] : ""; string address = (Request.Form["address"] != null) ? Request.Form["address"] : ""; string address2 = (Request.Form["address2"] != null) ? Request.Form["address2"] : ""; string city = (Request.Form["city"] != null) ? Request.Form["city"] : ""; int stateID = (Request.Form["state"] != null) ? Convert.ToInt32(Request.Form["state"]) : 0; string postalCode = (Request.Form["postal_code"] != null) ? Request.Form["postal_code"] : ""; string contact = (Request.Form["contact_person"] != null) ? Request.Form["contact_person"] : ""; string website = (Request.Form["website"] != null) ? Request.Form["website"] : ""; string eLocalURL = (Request.Form["eLocalURL"] != null) ? Request.Form["eLocalURL"] : ""; string searchURL = (Request.Form["searchURL"] != null) ? Request.Form["searchURL"] : ""; string logo = (Request.Form["logo"] != null && Request.Form["logo"].Trim() != "") ? Request.Form["logo"] : null; int dealer_type = (Request.Form["dealer_type"] != null) ? Convert.ToInt32(Request.Form["dealer_type"]) : 0; int customerID = (Request.Form["customerID"] != null && Request.Form["customerID"] != "") ? Convert.ToInt32(Request.Form["customerID"]) : 0; int salesRepID = (Request.Form["salesRepID"] != null && Request.Form["salesRepID"] != "") ? Convert.ToInt32(Request.Form["salesRepID"]) : 0; int mapixCodeID = (Request.Form["mapixCodeID"] != null && Request.Form["mapixCodeID"] != "") ? Convert.ToInt32(Request.Form["mapixCodeID"]) : 0; int parentID = (Request.Form["parentID"] != null && Request.Form["parentID"] != "") ? Convert.ToInt32(Request.Form["parentID"]) : 0; int tier = (Request.Form["tier"] != null && Request.Form["tier"] != "") ? Convert.ToInt32(Request.Form["tier"]) : 1; bool isDummy = (Request.Form["isDummy"] != null && Request.Form["isDummy"] != "") ? Convert.ToBoolean(Request.Form["isDummy"]) : false; bool showWebsite = (Request.Form["showWebsite"] != null && Request.Form["showWebsite"] != "") ? Convert.ToBoolean(Request.Form["showWebsite"]) : false; LatLng location = new LatLng(); // Validate the form fields if (name.Length == 0) throw new Exception("Name is required."); if (customerID == 0 && parentID == 0) throw new Exception("Either a Customer ID or a Parent Customer is required."); if (dealer_type == 0) throw new Exception("A Dealer Type is required."); if (address != "" && city != "" && stateID != 0) { GoogleMaps map = new GoogleMaps(System.Configuration.ConfigurationManager.AppSettings["GoogleMapsKey"]); location = map.GetLatLng(address, city, stateID); } // Create the new customer and save Customer new_cust = new Customer { name = name, email = email, phone = phone, fax = fax, address = address, address2 = address2, city = city, stateID = stateID, postal_code = postalCode, contact_person = contact, website = website, eLocalURL = eLocalURL, searchURL = searchURL, logo = logo, mCodeID = mapixCodeID, salesRepID = salesRepID, dealer_type = dealer_type, isDummy = isDummy, tier = tier, showWebsite = showWebsite }; if (location.latitude != null && location.longitude != null) { new_cust.latitude = location.latitude; new_cust.longitude = location.longitude; } if (customerID != 0) { new_cust.customerID = customerID; } if (parentID != 0) { new_cust.parentID = parentID; } db.Customers.InsertOnSubmit(new_cust); db.SubmitChanges(); return RedirectToAction("Add"); } } catch (Exception e) { error = e.Message; } #endregion // Get the dealer tiers List<DealerTier> dealer_tiers = CustomerModel.GetDealerTiers(); ViewBag.dealer_tiers = dealer_tiers; // Get the dealer types List<DealerType> dealer_types = CustomerModel.GetDealerTypes().Reverse<DealerType>().ToList<DealerType>(); ViewBag.dealer_types = dealer_types; // Get the mapix codes List<MapixCode> mapix_codes = CustomerModel.GetMapixCodes(); ViewBag.mapix_codes = mapix_codes; // Get the mapix codes List<SalesRepresentative> sales_reps = CustomerModel.GetSalesReps(); ViewBag.sales_reps = sales_reps; // Get the states List<FullCountry> countries = CustomerModel.GetCountries(); ViewBag.countries = countries; // Get the customer list List<Customer> customers = CustomerModel.GetAll(); ViewBag.customers = customers; ViewBag.error = error; return View(); }