根据经纬度计算两点之间的距离

package com.thinkgem.jeesite.modules.business.utils;
/**
 * @author 罗超
 * @create 2018-03-20 上午7:13
 **/
public class Distance {
    //地球平均半径
    private static final double EARTH_RADIUS = 6376984;
    //把经纬度转为度(°)
    private static double rad(double d){
        return d * Math.PI / 180.0;
    }

    /**
     * 根据两点间经纬度坐标(double值),计算两点间距离,单位为米
     * @param lng1
     * @param lat1
     * @param lng2
     * @param lat2
     * @return
     */
    public static double getDistance(double lng1, double lat1, double lng2, double lat2){
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        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;
    }

    /**
     * 中国大地原点纬度
     */
    private static double chinaGeodeticOriginLat = 34.3227;
    /**
     * 用椭圆的参数方程
     * x=acosθ=6378*cosθ
     * y=bsinθ=6357*sinθ
     * 将θ=37°带入
     * x=5093.70
     * y=3825.74
     * 点(5093.70 , 3825.74)到原点(0,0)的距离d=√(x^2+y^2) =6370.41 km
     */
    private static double getRadius() {
        double x = 6378140*Math.cos(chinaGeodeticOriginLat);
        double y = 6356755*Math.sin(chinaGeodeticOriginLat);
        return Math.sqrt(x*x+y*y);
    }

    /**
     * test
     * @param args
     */
    public static void main(String[] args){
        double distance = getDistance(121.491909,31.233234,121.411994,31.206134);
        System.out.println("Distance is:"+distance);
        double radius = getRadius();
        System.out.println("Radius is:"+radius);
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值