两点GPS经纬度计算

lat为纬度23.,lng为经度:113.

private const float EARTH_RADIUS = 6378.137f;//地球半径

    public static float Distance(float lat1, float lng1, float lat2, float lng2)
    {
        // Debug.Log ("----------" + lat1 + " " + lng1 + " " + lat2 + " " + lng2);
        float radLat1 = rad(lat1);
        float radLat2 = rad(lat2);
        float a = radLat1 - radLat2;
        float b = rad(lng1) - rad(lng2);


        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 * EARTH_RADIUS;
        //单位为米
        s = Mathf.Round(s * 10000) / 10;
        //单位为千米
        // s = Mathf.Round(s * 10000) / 10000;
        return s;
    }
    public static float Distance(string _lat1, string _lng1, string _lat2, string _lng2)
    {
        float lat1 = float.Parse(_lat1);
        float lng1 = float.Parse(_lng1);
        float lat2 = float.Parse(_lat2);
        float lng2 = float.Parse(_lng2);
        // Debug.Log ("----------" + lat1 + " " + lng1 + " " + lat2 + " " + lng2);
        float radLat1 = rad(lat1);
        float radLat2 = rad(lat2);
        float a = radLat1 - radLat2;
        float b = rad(lng1) - rad(lng2);


        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)));
        Debug.Log("lat1=" + lat1 + " lng1=" + lng1 + " a=" + a + " b=" + b + "  s=" + s);
        s = s * EARTH_RADIUS;
        //单位为米
        s = Mathf.Round(s * 10000) / 10;
        //单位为千米
        //s = Mathf.Round(s * 10000) / 10000;
        return s;
    }
    private static float rad(float d)
    {
        return d * Mathf.PI / 180.0f;

    }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值