Пример #1
0
        private static Coordinates GeocodeLocation(string address)
        {
            Coordinates     coordinates    = new Coordinates();
            IGeocodeRequest geocodeRequest = new GeocodeRequest(address);

            GoogleGeocoderV3 geocodeService = new GoogleGeocoderV3();
            var geocodeResponse             = geocodeService.Geocode(geocodeRequest);

            Thread.Sleep(500);

            if (geocodeResponse.Success)
            {
                coordinates.Latitude       = (float)geocodeResponse.Lat;
                coordinates.Longitude      = (float)geocodeResponse.Long;
                coordinates.StatusMessage  = String.Format("Successfully geocoded {0}", address);
                coordinates.GeocodeSuccess = true;
                return(coordinates);
            }
            else
            {
                coordinates.GeocodeSuccess = false;
                coordinates.StatusMessage  = String.Format("FAILED to geocode {0}. Response: {1}", address, geocodeResponse.ErrorMessage);
                return(coordinates);
            }
        }
Пример #2
0
        protected void btnEstimateShipping_Click(object sender, EventArgs e)
        {
            ShipmentPackagingStrategy shipmentPackagingStrategy = WA.Enum <ShipmentPackagingStrategy> .TryParseOrDefault(StoreContext.CurrentStore.GetSetting(StoreSettingNames.ShipmentPackagingStrategy), ShipmentPackagingStrategy.SingleBox);

            IPostalAddress origin      = StoreContext.CurrentStore.Address.ToPostalAddress();
            IPostalAddress destination = new PostalAddress()
            {
                PostalCode = txtShippingEstimateZip.Text.Trim(), IsResidential = !chkShippingAddressIsBusiness.Checked
            };

            var shipmentPackages = checkoutOrderInfo.Cart.GetCartItemsAsShipmentPackages(shipmentPackagingStrategy);
            var shippingRates    = new List <IShippingRate>();
            var shippingServices = StoreContext.CurrentStore.GetEnabledShippingProviders(null, checkoutOrderInfo.Cart.Id);

            foreach (var shipper in shippingServices)
            {
                //if(shipper is UpsShippingService)
                //{
                // NOTE: UPS is silly and requires PostalCode AND State/Region - Now being used for all shipping estimates for a more accurate estimate
                IGeocodeRequest request  = new GoogleGeocodeRequest(destination.PostalCode);
                IGeocodeService service  = new GoogleGeocoderV3();
                var             response = service.Geocode(request) as GoogleGeocodeResponse;
                if (response.Success && response.Results.Length > 0)
                {
                    string region = response.Results[0].Address_Components.Where(x => x.Types.Contains(AddressComponentType.administrative_area_level_1)).Select(x => x.Short_Name).FirstOrDefault();
                    if (!string.IsNullOrEmpty(region))
                    {
                        destination.Region = region;
                    }

                    string countryCode = response.Results[0].Address_Components.Where(x => x.Types.Contains(AddressComponentType.country)).Select(x => x.Short_Name).FirstOrDefault();
                    if (!string.IsNullOrEmpty(countryCode))
                    {
                        destination.CountryCode = countryCode;
                    }
                }
                //}
                try
                {
                    shippingRates.AddRange(shipper.GetRates(origin, destination, shipmentPackages));
                }
                catch (Exception ex)
                {
                    Exceptions.LogException(ex);
                    ShowFlash(ex.Message);
                }
            }

            //rptShippingRateEstimates.DataSource = shippingRates;
            //rptShippingRateEstimates.DataBind();

            rblShippingRateEstimates.Items.Clear();
            shippingRates.ForEach(x => rblShippingRateEstimates.Items.Add(new ListItem()
            {
                Value = string.Format(@"{0}||{1}||{2}", x.ServiceType, x.ServiceTypeDescription, x.Rate),
                Text  = string.Format(@"{0} - {1}", ((ShippingRate)x).DisplayName, HttpUtility.HtmlDecode(StoreContext.CurrentStore.FormatCurrency(x.Rate)))
            }));
        }