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千米

    }
    }

  • 相关阅读:
    springmvc log4j 配置
    intellij idea maven springmvc 环境搭建
    spring,property not found on type
    intellij idea maven 工程生成可执行的jar
    device eth0 does not seem to be present, delaying initialization
    macos ssh host配置及免密登陆
    centos7 搭建 docker 环境
    通过rest接口获取自增id (twitter snowflake算法)
    微信小程序开发体验
    gitbook 制作 beego 参考手册
  • 原文地址:https://www.cnblogs.com/zlazm/p/9273067.html
Copyright © 2011-2022 走看看