zoukankan      html  css  js  c++  java
  • 计算两个经纬度的距离

    上海市浦东新区乳山路231号
    121.533084,31.242315
    上海市浦东新区锦绣路1001号
    121.558979,31.222046

    用sql计算:

    SELECT (acos(sin((31.242315*3.1415)/180) * sin((31.222046*3.1415)/180) + cos((31.242315*3.1415)/180) * cos((31.222046*3.1415)/180) * cos((121.533084*3.1415)/180 - (121.558979*3.1415)/180))*6378.137)

    参考
    select * from 表名 where status=1 and isopen =0 and jingyingtype=1 and waimai=1 and bstatus = 1 and (acos(sin(({$httpData['lat']}*3.1415)/180) * sin((lat*3.1415)/180) + cos(({$httpData['lat']}*3.1415)/180) * cos((lat*3.1415)/180) * cos(({$httpData['lng']}*3.1415)/180 - (lng*3.1415)/180))*6370.996)<=5

    用C#代码:

     //地球半径,单位米
            private const double EARTH_RADIUS = 6378137;
            /// <summary>
            /// 计算两点位置的距离,返回两点的距离,单位 米
            /// 该公式为GOOGLE提供,误差小于0.2米
            /// </summary>
            /// <param name="lat1">第一点纬度</param>
            /// <param name="lng1">第一点经度</param>
            /// <param name="lat2">第二点纬度</param>
            /// <param name="lng2">第二点经度</param>
            /// <returns></returns>
            public static double GetDistance(double lat1, double lng1, double lat2, double lng2)
            {
                double radLat1 = Rad(lat1);
                double radLng1 = Rad(lng1);
                double radLat2 = Rad(lat2);
                double radLng2 = Rad(lng2);
                double a = radLat1 - radLat2;
                double b = radLng1 - radLng2;
                double result = 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))) * EARTH_RADIUS;
    
                //var result = Math.Round(cal * 10000) / 10000;     //保留小数点后四位
                return result;
            }
    
            /// <summary>
            /// 经纬度转化成弧度
            /// </summary>
            /// <param name="d"></param>
            /// <returns></returns>
            private static double Rad(double d)
            {
                return (double)d * Math.PI / 180d;
            }

    调用 :

    var sec = GetDistance(31.242315, 121.533084, 31.222046, 121.558979);
    Console.WriteLine(sec);

    参考:

    https://www.cnblogs.com/chenkh/p/5661097.html

    http://www.itpow.com/c/2016/11/6690.asp

    https://www.cnblogs.com/love1226/p/11345351.html

    此随笔或为自己所写、或为转载于网络。仅用于个人收集及备忘。

  • 相关阅读:
    css之overflow注意事项,分析效果没有实现的原因及解决
    Leetcode- 299. Bulls and Cows
    Leetcode-234. Palindrome Linked List
    Leetcode-228 Summary Ranges
    Leetcode-190. Reverse Bits
    盒子模型的理解
    css各类伪元素总结以及清除浮动方法总结
    Leetcode-231. Power of Two
    Uncaught TypeError: __WEBPACK_IMPORTED_MODULE_0_vue__.default.user is not a
    git commit -m ''后报eslint错误
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/14923392.html
Copyright © 2011-2022 走看看