internal Address GeoLocate()
 {
     GeocodingResponse geo = Geocoding.GetGeoLocation(this.street1 + " " + this.street2, this.city, 0, this.postal_code, this.State1.Country.abbr, this.State1.abbr);
     LatitudeLongitude latLon = new LatitudeLongitude();
     if (geo.results.Count > 0) {
         latLon = geo.results[0].geometry.location;
         this.latitude = latLon.lat;
         this.longitude = latLon.lng;
     }
     return this;
 }
Exemple #2
0
        internal static void Save(int id, string Name, string Phone, string Fax, string Street1, string Street2, string City, int State, string PostalCode, out DistributionCenter dc, out List<string> errors)
        {
            EcommercePlatformDataContext db = new EcommercePlatformDataContext();
            dc = new DistributionCenter();
            errors = new List<string>();

            if (id != 0) {
                dc = db.DistributionCenters.Where(x => x.ID.Equals(id)).FirstOrDefault<DistributionCenter>();
            }
            try {
                // Validate the fields
                if (Name.Length == 0) { throw new Exception(); } else { dc.Name = Name; }
                if (Phone.Length == 0) { throw new Exception(); } else { dc.Phone = Phone; }
                if (Street1.Length == 0) { throw new Exception(); } else { dc.Street1 = Street1; }
                if (City.Length == 0) { throw new Exception(); } else { dc.City = City; }
                if (State == 0) { throw new Exception(); } else { dc.State = State; }
                if (PostalCode.Length == 0) { throw new Exception(); } else { dc.PostalCode = PostalCode; }
                dc.Fax = Fax;
                dc.Street2 = Street2;

                // Geocode the DC
                GeocodingResponse geo = Geocoding.GetGeoLocation(dc.Street1 + " " + dc.Street2, dc.City, 0, dc.PostalCode, dc.State1.Country.abbr, dc.State1.abbr);
                LatitudeLongitude latLon = new LatitudeLongitude();
                if (geo.results.Count > 0) {
                    latLon = geo.results[0].geometry.location;
                    dc.Latitude = latLon.lat;
                    dc.Longitude = latLon.lng;
                } else {
                    errors.Add("Failed to retrieve geographical location.");
                }

                if (errors.Count == 0) {
                    if (id == 0) {
                        db.DistributionCenters.InsertOnSubmit(dc);
                    }
                    db.SubmitChanges();
                }
            } catch (Exception e) {
                errors.Add(e.Message);
            }
        }
        internal static void Save(int id, string name, string phone, string fax, string email, string address, string city, int stateID, int zip, int isPrimary, int google_places, int foursquare, out Location loc, out List<string> errors)
        {
            EcommercePlatformDataContext db = new EcommercePlatformDataContext();
            Dictionary<List<string>, Location> response = new Dictionary<List<string>, Location>();
            loc = new Location();
            errors = new List<string>();

            if (id != 0) {
                loc = db.Locations.Where(x => x.locationID.Equals(id)).FirstOrDefault<Location>();
            }

            // Validate the fields
            if (name.Length == 0) { throw new Exception(); } else { loc.name = name; }
            if (phone.Length == 0) { throw new Exception(); } else { loc.phone = phone; }
            if (email.Length == 0) { throw new Exception(); } else { loc.email = email; }
            if (address.Length == 0) { throw new Exception(); } else { loc.address = address; }
            if (city.Length == 0) { throw new Exception(); } else { loc.city = city; }
            if (stateID == 0) { throw new Exception(); } else { loc.stateID = stateID; }
            if (zip == 0) { throw new Exception(); } else { loc.zip = zip; }
            loc.isPrimary = isPrimary;
            loc.fax = fax;

            GeocodingResponse geo = Geocoding.GetGeoLocation(loc.address, loc.city, stateID, zip.ToString());
            LatitudeLongitude lat_lon = new LatitudeLongitude();
            if (geo.results.Count > 0) {
                lat_lon = geo.results[0].geometry.location;
                loc.latitude = lat_lon.lat;
                loc.longitude = lat_lon.lng;
            } else {
                errors.Add("Failed to retrive geographical location.");
            }

            if (google_places == 1) {
                try {
                    string form_types = HttpContext.Current.Request.Form["place_types"];
                    List<string> place_types = new List<string>();
                    if (form_types != null) {
                        if (form_types.Contains(',')) {
                            place_types = form_types.Split(',').ToList<string>();
                        } else {
                            place_types.Add(form_types);
                        }
                    }

                    LatitudeLongitude place_latlng = new LatitudeLongitude {
                        lat = loc.latitude,
                        lng = loc.longitude
                    };

                    NewPlace place = new NewPlace {
                        location = place_latlng,
                        accuracy = 50,
                        name = loc.name,
                        types = place_types,
                        language = "en"
                    };

                    var returned_place = Geocoding.AddPlace(place);
                    try {
                        AddPlaceResponse new_place = new AddPlaceResponse();
                        try {
                            new_place = (AddPlaceResponse)returned_place;
                        } catch (Exception) {
                            throw new Exception(returned_place);
                        }

                        if (new_place.status != "OK") {
                            throw new Exception("Google Places request was denied");
                        }

                        loc.places_status = new_place.status;
                        loc.places_reference = new_place.reference;
                        loc.places_id = new_place.id;

                    } catch (Exception e) {
                        errors.Add(e.Message);
                    }
                } catch (Exception) {
                    errors.Add("Failed to submit Google Places listing");
                }
            }
            if (errors.Count == 0) {
                if (id == 0) {
                    db.Locations.InsertOnSubmit(loc);
                }
                db.SubmitChanges();
            }
            //response.Add(errors, loc);
            //return response;
        }