• 关于知道2个经纬度计算距离的2个计算方法


     1     public static double getDistance(double lat1, double lng1, double lat2, double lng2) {
     2         double radLat1 = rad(lat1);
     3         double radLat2 = rad(lat2);
     4         double a = radLat1 - radLat2;
     5         double b = rad(lng1) - rad(lng2);
     6         double s = 2 * Math.asin(Math.sqrt(
     7                 Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
     8         s = s * EARTH_RADIUS;
     9         s = Math.round(s * 10000d) / 10000d;
    10         s = s * 1000;
    11         return s;
    12     }

    这个方法在网上找到的,很多,相对是比较精确的。但是当我们只需要关心的是短距离的。那完全可以把地球看成是个近似平面。这个和我们平时在路上走的感觉一样。

    地球是平的

    有这个近似计算量就缩小了很多。

    1 /**
    2      * 近似算法,在短距离情况下,地球表面可以近似于平面,以三角计算法计算即可
    3      * 与getDistance相比,此方法计算时间大概缩小100倍
    4      * @return
    5      */
    6     public static double getDistanceSimple(double lat1, double lng1, double lat2, double lng2) {
    7         double distance=Math.sqrt(Math.pow((lng1-lng2), 2)+Math.pow((lat1-lat2), 2));
    8         return distance*110193;  // 1°对应的距离,大概11公里
    9     }
    1 public static void main(String [] args) {
    2         long s=System.nanoTime();
    3         Double d1=getDistance(30.245317899331223,120.17062046716632,30.249613375259734,120.17361255101852);
    4         long e=System.nanoTime();
    5         Double d2=getDistanceSimple(30.245317899331223,120.17062046716632,30.249613375259734,120.17361255101852);
    6         long f=System.nanoTime();
    7         System.out.println(d1.intValue()+" "+(e-s)*1.0/1000+"ns");
    8         System.out.println(d2.intValue()+" "+(f-e)*1.0/1000+"ns");
    9     }
    558 172.849ns
    576 1.921ns

    距离差几米,计算时间差100倍左右

  • 相关阅读:
    Mysql Bypass小结
    SQLite手工注入方法小结
    bodgeit测试平台
    Sqlite注入测试
    ASP代码审计学习笔记 -3.上传漏洞
    centos7环境下apache2.2.34的编译安装
    centos7.6环境下编译安装tengine-2.2.2的编译安装
    centos7.6编译安装php7.2.11及redis/memcached/rabbitmq/openssl/curl等常见扩展
    Windows2016的 IIS中配置PHP7运行环境
    服务器资源迁移到aliyun对象存储及oss的权限管理配置
  • 原文地址:https://www.cnblogs.com/nanahome/p/9182845.html
Copyright © 2020-2023  润新知