坐标距离计算

1.球面计算

public static void main(String[] args) {
        double x = 104.07966;
        double y = 30.581751;

        double x1 = 104.072833;
        double y1 = 30.58172;

        System.out.println(getDistance(x,y,x1,y1));
    }

    /**
     * 角度弧度计算公式 rad
     * 360度=2π π=Math.PI
     * x度 = x*π/360 弧度
     * @param degree
     * @since JDK 1.8
     */
    private static double getRadian(double degree) {
        return degree * Math.PI / 180.0;
    }

    /**
     * 根据经纬度计算两点之间的距离 GetDistance
     * @param lat1 1纬度
     * @param lng1 1经度
     * @param lat2 2纬度
     * @param lng2 2经度
     * @return 距离 单位 米
     * @since JDK 1.8
     */
    public static double getDistance(double lat1, double lng1, double lat2, double lng2){
        double radLat1 = getRadian(lat1);
        double radLat2 = getRadian(lat2);
        double a = radLat1 - radLat2;// 两点纬度差
        double b = getRadian(lng1) - getRadian(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 * 6371000;
        return s * ;
    }

计算所得结果为:759.128米

2.GlobalCoordinates计算方式:

引入maven依赖包

<dependency>
	<groupId>org.gavaghan</groupId>
	<artifactId>geodesy</artifactId>
	<version>1.1.3</version>
</dependency>
public class CaculateDistanceTest{

    public static void main(String[] args){

        GlobalCoordinates source = new GlobalCoordinates(30.581751, 104.07966);
        GlobalCoordinates target = new GlobalCoordinates(30.58172, 104.072833);

        double meter1 = getDistanceMeter(source, target, Ellipsoid.Sphere);
        double meter2 = getDistanceMeter(source, target, Ellipsoid.WGS84);

        System.out.println("Sphere计算结果:"+meter1 + "米");
        System.out.println("WGS84计算结果:"+meter2 + "米");
    }

    public static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid){

        //创建GeodeticCalculator,调用计算方法,传入坐标系、经纬度用于计算距离
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);

        return geoCurve.getEllipsoidalDistance();
    }
}

计算结果为:

Sphere坐标系计算结果:653.545
WGS84坐标系计算结果:654.845

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值