internal CouponType FindNearestCoupon(List<CouponType> couponsList, Location location)
{
if (couponsList == null || couponsList.Count == 0 || location == null)
{
log.WarnFormat("[FindNearestCoupon] couponsList is null or empty or location=null");
return null;
}
CouponType returnCoupon = null;
double minimalDistance = 1; //Max distance in Radians
foreach (CouponType coupon in couponsList)
{
if (coupon.LocationsList != null && coupon.LocationsList[0] != null)
{
double e = (3.1415926538 * location.Latitude / 180);
double f = (3.1415926538 * location.Longitude / 180);
double g = (3.1415926538 * coupon.LocationsList[0].Latitude / 180);
double h = (3.1415926538 * coupon.LocationsList[0].Longitude / 180);
double i = (Math.Cos(e) * Math.Cos(g) * Math.Cos(f) * Math.Cos(h) + Math.Cos(e) * Math.Sin(f) * Math.Cos(g) * Math.Sin(h) + Math.Sin(e) * Math.Sin(g));
double j = (Math.Acos(i));
if (minimalDistance > j)
{
returnCoupon = coupon;
minimalDistance = j;
}
}
//else
//{
// log.WarnFormat("[FindNearestCoupon] tried to get nearest coupon, but one of coupons has NULL location.");
// return null;
//}
}
return returnCoupon;
}