经纬度两点之间的距离计算

public static float GetDistance(LocationInfo info1, LocationInfo info2)
{
if(Mathf.Abs(info1.latitude) > 90 || Mathf.Abs(info2.latitude) > 90)
{
return 0;
}

    if(Mathf.Abs(info2.longitude) > 180 || Mathf.Abs(info2.longitude) > 180)
    {
        return 0;
    }

    float radLat1 = rad(info1.latitude);
    float radLat2 = rad(info2.latitude);

    float a = radLat1 - radLat2;
    float b = rad(info1.longitude) - rad(info2.longitude);
    float s = 2 * Mathf.Asin(Mathf.Sqrt(Mathf.Pow(Mathf.Sin(a / 2), 2) + Mathf.Cos(radLat1) * Mathf.Cos(radLat2) * Mathf.Pow(Mathf.Sin(b / 2), 2)));
    s = s * 6378.137f;
    s = Mathf.Round(s * 10000) / 10000;

    return s;
}

private static float rad(float d)
{
return d * Mathf.PI / 180.0f;
}

你可能感兴趣的:(技术)