113 lines
3.8 KiB
113 lines
3.8 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
namespace GPSServer.Helper
|
|
{
|
|
public class CalculationGpsHelper
|
|
{
|
|
/// <summary>
|
|
/// 地球的半径
|
|
/// </summary>
|
|
private const double EarthRadius = 6378137.0;//地球半径(米)
|
|
/// <summary>
|
|
/// 角度数转换为弧度公式
|
|
/// </summary>
|
|
/// <param name="d"></param>
|
|
/// <returns></returns>
|
|
private static double Radians(double d)
|
|
{
|
|
return d * Math.PI / 180.0;
|
|
}
|
|
/// <summary>
|
|
/// 弧度转换为角度数公式
|
|
/// </summary>
|
|
/// <param name="d"></param>
|
|
/// <returns></returns>
|
|
private static double Degrees(double d)
|
|
{
|
|
return d * (180 / Math.PI);
|
|
}
|
|
// 计算2个经纬度距离
|
|
public static int GetDistance(double lng1, double lat1, double lng2, double lat2)
|
|
{
|
|
//最东端 东经135度2分30秒 黑龙江和乌苏里江交汇处
|
|
//最西端 东经73度40分 帕米尔高原乌兹别里山口(乌恰县)
|
|
//最南端 北纬3度52分 南沙群岛曾母暗沙
|
|
//最北端 北纬53度33分 漠河以北黑龙江主航道(漠河)
|
|
try
|
|
{
|
|
if (lng1 > 73 && lat1 > 3 && lng2 > 73 && lat2 > 3)
|
|
{
|
|
decimal proDecimal = (decimal)CalculationDistance(lng1, lat1, lng2, lat2);
|
|
return Convert.ToInt32(proDecimal);
|
|
}
|
|
return 0;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 计算坐标点的距离
|
|
/// </summary>
|
|
/// <param name="long1">开始的经度</param>
|
|
/// <param name="lat1">开始的纬度</param>
|
|
/// <param name="long2">结束的经度</param>
|
|
/// <param name="lat2">结束的纬度</param>
|
|
/// <returns>距离(米)</returns>
|
|
public static double CalculationDistance(double long1, double lat1, double long2, double lat2)
|
|
{
|
|
double a, b, R;
|
|
R = EarthRadius; //地球半径
|
|
lat1 = Radians(lat1);
|
|
lat2 = Radians(lat2);
|
|
a = lat1 - lat2;
|
|
b = Radians(long1-long2);
|
|
double d, sa2, sb2;
|
|
sa2 = Math.Sin(a / 2.0);
|
|
sb2 = Math.Sin(b / 2.0);
|
|
d = 2 * R * Math.Asin(Math.Sqrt(sa2 * sa2 + Math.Cos(lat1) * Math.Cos(lat2) * sb2 * sb2));
|
|
return d;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 时间差格式转换
|
|
/// </summary>
|
|
/// <param name="ts">时间差</param>
|
|
/// <param name="Accuracy">精确度,默认为2</param>
|
|
/// <returns></returns>
|
|
public static string TimeSpanFormat(TimeSpan ts, int Accuracy = 2)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
int idx = 0;
|
|
if (ts.Days != 0 && idx < Accuracy)
|
|
{
|
|
sb.Append(ts.Days + "天");
|
|
idx++;
|
|
}
|
|
if (ts.Hours != 0 && idx < Accuracy)
|
|
{
|
|
sb.Append(ts.Hours + "小时");
|
|
idx++;
|
|
}
|
|
if (ts.Minutes != 0 && idx < Accuracy)
|
|
{
|
|
sb.Append(ts.Minutes + "分");
|
|
idx++;
|
|
}
|
|
if (ts.Seconds != 0 && idx < Accuracy)
|
|
{
|
|
sb.Append(ts.Seconds + "秒");
|
|
}
|
|
if (ts.Days == 0 && ts.Hours == 0 && ts.Minutes == 0 && ts.Seconds == 0)
|
|
{
|
|
sb.Append("0秒");
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
}
|
|
|