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

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

  • 相关阅读:
    3.31考试T2
    P3225 [HNOI2012]矿场搭建
    P2714 四元组统计
    3.29考试T2 变戏法
    开发中可能会用到的几个小tip----QT, pycharm, android, 等
    pycharm下: conda installation is not found ----一个公开的bug的解决方案
    ubuntu16.04 opencv3.4.1 opencv-contribute3.4.1 compile
    vs 2015 update 3各版本下载地址
    在金融业工作了六年,给想入这行的说几个经验
    在centos上安装smplayer播放器
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/14923392.html
Copyright © 2011-2022 走看看