/// <summary> /// 计算距离,如返回最大值需要检查数据 /// </summary> public static double GetDistanceInKm(GeoInfo startPoint, GeoInfo endPoint, double?defaultValue) { if (!IsGeoValid(startPoint) || !IsGeoValid(endPoint)) { return(defaultValue ?? throw new Exception("无法计算距离,传入坐标错误")); } var startlatrad = Rad(startPoint.Lat); var endlatrad = Rad(endPoint.Lat); var a = startlatrad - endlatrad; var b = Rad(startPoint.Lon - endPoint.Lon); return(2 * EARTH_RADIUS * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(startlatrad) * Math.Cos(endlatrad) * Math.Pow(Math.Sin(b / 2), 2)))); }
/// <summary> /// 验证经纬度数值正确性 /// </summary> private static bool IsGeoValid(GeoInfo point) => point != null && Math.Abs(point.Lat) <= 90 && Math.Abs(point.Lon) <= 180;