/// <summary>
    /// Retrieve lat/lon for either the selected location, or the one entered in the text box.
    /// If a custom location is used then the result is cached for a few minutes to avoid multiple queries to the geocoding
    /// service.
    /// </summary>
    /// <param name="lat"></param>
    /// <param name="lon"></param>
    /// <returns></returns>
    private bool GetLatLonForLocation(out double lat, out double lon)
    {
        IAddress addr = null;

        if (cbxLocation.SelectedValue == LOCATION_CUSTOM)
        {
            addr = EntityFactory.Create <IAddress>();
            if (string.IsNullOrEmpty(txtCustom.Text))
            {
                throw new ValidationException(GetLocalResourceObject("Custom_MissingLocation").ToString());
            }
            string cacheKey = "ProximitySearch$Geocode$" + HttpUtility.UrlEncode(txtCustom.Text);
            if (Cache[cacheKey] != null)
            {
                addr = (IAddress)Cache[cacheKey];
            }
            else
            {
                // Just put the whole string in the address field
                addr.Address1 = txtCustom.Text;
                double?latitude, longitude;

                IGeocodeProvider provider = Saleslogix.Geocode.Managers.ProviderManager.GetProvider();
                if (provider != null && provider.GeocodeAddress(txtCustom.Text, out latitude, out longitude))
                {
                    addr.GeocodeLatitude  = latitude.Value;
                    addr.GeocodeLongitude = longitude.Value;
                    addr.GeocodeProvider  = provider.Source;

                    Cache.Add(cacheKey, addr, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(4),
                              System.Web.Caching.CacheItemPriority.Normal, null);
                }
            }
            if (!addr.IsGeocoded())
            {
                throw new ValidationException(GetLocalResourceObject("Custom_UnableToLocate").ToString());
            }
        }
        else
        {
            string addrId = cbxLocation.SelectedValue;
            addr = EntityFactory.GetById <IAddress>(addrId);
            if (addr == null || !addr.IsGeocoded())
            {
                throw new ValidationException(GetLocalResourceObject("Custom_AddressNotLocated").ToString());
            }
        }

        if (addr != null)
        {
            lat = addr.GeocodeLatitude.Value;
            lon = addr.GeocodeLongitude.Value;
            return(true);
        }

        lat = 0;
        lon = 0;
        return(false);
    }
예제 #2
0
 internal static bool GeocodeAddress(IAddress address)
 {
     try
     {
         if (address.GeocodeFailed != null && address.GeocodeFailed.Value)
         {
             return(false);
         }
         IGeocodeProvider provider = Saleslogix.Geocode.Managers.ProviderManager.GetProvider(null, null);
         if (provider != null)
         {
             return(provider.GeocodeAddress(address));
         }
     }
     catch
     {
         return(false);
     }
     return(false);
 }