计算纬度、经度距离,返回公里(千米)数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClDing.Utility
{
/// <summary>
/// 计算纬度、经度距离
/// </summary>
public class Distance
{
private const double EARTH_RADIUS = 6378.137;//地球半径
private static double Rad(double d)
{
return d * Math.PI / 180.0;
}
/// <summary>
/// 计算纬度、经度距离,返回公里(千米)数
/// </summary>
/// <param name="lat1">纬度1</param>
/// <param name="lng1">经度1</param>
/// <param name="lat2">纬度2</param>
/// <param name="lng2">经度2</param>
/// <returns></returns>
public static double Comput(double lat1, double lng1, double lat2, double lng2)
{
double radLat1 = Rad(lat1);
double radLat2 = Rad(lat2);
double a = Math.Abs(radLat1 - radLat2);
double b = Math.Abs(Rad(lng1) - Rad(lng2));
double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) +
Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2)));
s = s * EARTH_RADIUS;
s = Math.Round(s * 10000, 0) / 10000;
return s;
}
}
}