GpsHelper


C# 全选
/// <summary>
/// GPS 帮助类
/// </summary>
public class GpsHelper
{
	private static double EARTH_RADIUS;

	static GpsHelper()
	{
		GpsHelper.EARTH_RADIUS = 6371393;
	}

	public GpsHelper()
	{
	}

	/// <summary>
	/// 将角度换算为弧度。
	/// </summary>
	/// <param name="degrees">角度</param>
	/// <returns>弧度</returns>
	public static double ConvertDegreesToRadians(double degrees)
	{
		return degrees * 3.14159265358979 / 180;
	}

	public static double ConvertRadiansToDegrees(double radian)
	{
		return radian * 180 / 3.14159265358979;
	}

	/// <summary>
	/// 计算两点GPS坐标的距离(单位:米)
	/// </summary>
	/// <param name="n1">第一点的纬度坐标</param>
	/// <param name="e1">第一点的经度坐标</param>
	/// <param name="n2">第二点的纬度坐标</param>
	/// <param name="e2">第二点的经度坐标</param>
	/// <returns></returns>
	public static double Distance(double n1, double e1, double n2, double e2)
	{
		double num = GpsHelper.Rad(n1);
		double num1 = GpsHelper.Rad(n2);
		double num2 = num - num1;
		double num3 = GpsHelper.Rad(e1) - GpsHelper.Rad(e2);
		return Math.Round(2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(num2 / 2), 2) + Math.Cos(num) * Math.Cos(num1) * Math.Pow(Math.Sin(num3 / 2), 2))) * 6378137 * 10000) / 10000;
	}

	/// <summary>
	/// 给定的经度1,纬度1;经度2,纬度2. 计算2个经纬度之间的距离。
	/// </summary>
	/// <param name="lat1">经度1</param>
	/// <param name="lon1">纬度1</param>
	/// <param name="lat2">经度2</param>
	/// <param name="lon2">纬度2</param>
	/// <returns>距离(公里、千米)</returns>
	public static double Distance2(double lat1, double lon1, double lat2, double lon2)
	{
		lat1 = GpsHelper.ConvertDegreesToRadians(lat1);
		lon1 = GpsHelper.ConvertDegreesToRadians(lon1);
		lat2 = GpsHelper.ConvertDegreesToRadians(lat2);
		lon2 = GpsHelper.ConvertDegreesToRadians(lon2);
		double num = Math.Abs(lon1 - lon2);
		double num1 = GpsHelper.HaverSin(Math.Abs(lat1 - lat2)) + Math.Cos(lat1) * Math.Cos(lat2) * GpsHelper.HaverSin(num);
		return 2 * GpsHelper.EARTH_RADIUS * Math.Asin(Math.Sqrt(num1));
	}

	/// <summary>
	/// 获取维度差
	/// </summary>
	/// <param name="km">千米</param>
	/// <returns></returns>
	public static double GetLatitudeDifference(double km)
	{
		return km * 1 / 111;
	}

	/// <summary>
	/// 获取经度差
	/// </summary>
	/// <param name="km">千米</param>
	/// <returns></returns>
	public static double GetLongitudeDifference(double km)
	{
		return km * 1 / 110;
	}

	public static double HaverSin(double theta)
	{
		double num = Math.Sin(theta / 2);
		return num * num;
	}

	private static double Rad(double d)
	{
		return d * 3.14159265358979 / 180;
	}
}
版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
管理员
上一篇:DateTimeHelper
评论列表

发表评论

评论内容
昵称:
关联文章