计算两个经纬度点的距离
程序员文章站
2024-02-14 23:03:46
...
/// <summary>
/// 经纬度计算距离
/// </summary>
/// <param name="x1"></param>
/// <param name="y1"></param>
/// <param name="x2"></param>
/// <param name="y2"></param>
/// <returns></returns>
public static double getLngLatDistance(double x1, double y1, double x2, double y2)
{
double pidiv180 = (double)Math.PI / 180;
double dLat1InRad = y1 * pidiv180;
double dLong1InRad = x1* pidiv180;
double dLat2InRad = y2 * pidiv180;
double dLong2InRad = x2 * pidiv180;
double dLongitude = dLong2InRad - dLong1InRad;
double dLatitude = dLat2InRad - dLat1InRad;
double a = (double)(Math.pow(Math.sin(dLatitude / 2), 2) +
Math.cos(dLat1InRad) * Math.cos(dLat2InRad) * Math.pow(Math.sin(dLongitude / 2), 2));
double c = (double)(2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)));
double dDistance = 6378137 * c;
return dDistance;
}
上一篇: Java中的Map——遍历Map的三种常用方式及遍历方法
下一篇: for(:)遍历数组的一种方式