zoukankan      html  css  js  c++  java
  • java根据经纬度计算距离

       在实际地图项目中会碰见距离的计算,这里写出来,以后参考:

      

    /**
    * @author 作者 :zhaoliang
    * @version : v1.0
    *创建时间:2018年6月26日 下午3:32:30
    * 类说明 :根据经纬度计算距离
    */
    public class DistanceUtil {

    private static double EARTH_RADIUS = 6378.137; //地球半径
    /**
    * 谷歌地图计算两个坐标点的距离
    * @param latitude 页面传递过来的经度
    * @param longitude 页面传递过来的纬度
    * @param gaode_lng 数据库中停车场的经度
    * @param gaode_lat 数据库中停车场的纬度
    * @return 距离(千米)
    */
    public static double getDistance(double latitude, double longitude, double gaodeLng, double gaodeLat) {
    double radLat1 = Math.toRadians(longitude);
    double radLat2 = Math.toRadians(gaodeLat);
    double a = radLat1 - radLat2;
    double b = Math.toRadians(latitude) - Math.toRadians(gaodeLng);
    double s = 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)));
    s = s * EARTH_RADIUS;
    s = Math.round(s * 10000) / 10000;
    return s;
    }

    public static void main(String[] args) {
    long b = System.currentTimeMillis();
    for(int i=0; i<1000000; i++){
    getDistance(116.403933,39.914147, 116.403237,39.927919);
    }
    System.out.println("耗时:"+(System.currentTimeMillis()-b)+"毫秒"); //耗时:461毫秒
    double dist = getDistance(116.403933,39.914147, 116.403237,39.927919);
    System.out.println("两点相距:" + dist + "千米"); //两点相距:1.0千米

    }
    }

  • 相关阅读:
    数据库操作
    jquery 第一章
    算法很美 第一章
    python 学习第四天
    python学习第三天
    python学习第二天
    学习python的第一天
    C#-线程
    C#-流、存储
    C#-集合
  • 原文地址:https://www.cnblogs.com/zlazm/p/9273067.html
Copyright © 2011-2022 走看看