java从地心到两个经纬度之间的夹角

 
  public static void main(String[] args) {
        // 北京的经纬度(北纬、东经)
        double latA = 39.9042; // 纬度
        double lonA = 116.4074; // 经度
	  
	  double latB = 18.5445; // 纬度
     double lonB = 17.65840; // 经度
	  
        // 上海的经纬度(北纬、东经)
     //   double latB = 31.2304; // 纬度
     //   double lonB = 121.4737; // 经度

        // 计算地心到两地的直线夹角
        double angle = calculateCentralAngle(latA, lonA, latB, lonB);

        // 输出结果
        System.out.printf("从地心到北京与上海的夹角约为: %.4f 度%n", angle);
    }

    /**
     * 计算地心到两地的直线夹角
     * @param latA 地点A的纬度
     * @param lonA 地点A的经度
     * @param latB 地点B的纬度
     * @param lonB 地点B的经度
     * @return 地心到两地的直线夹角(度)
     */
    public static double calculateCentralAngle(double latA, double lonA, double latB, double lonB) {
        // 将角度转换为弧度
        double latA_rad = Math.toRadians(latA);
        double lonA_rad = Math.toRadians(lonA);
        double latB_rad = Math.toRadians(latB);
        double lonB_rad = Math.toRadians(lonB);

        // 转换为三维空间坐标(假设地球半径为1)
        double xA = Math.cos(latA_rad) * Math.cos(lonA_rad);
        double yA = Math.cos(latA_rad) * Math.sin(lonA_rad);
        double zA = Math.sin(latA_rad);

        double xB = Math.cos(latB_rad) * Math.cos(lonB_rad);
        double yB = Math.cos(latB_rad) * Math.sin(lonB_rad);
        double zB = Math.sin(latB_rad);

        // 计算向量点积
        double dotProduct = xA * xB + yA * yB + zA * zB;

        // 计算夹角(弧度)
        double theta_rad = Math.acos(dotProduct);

        // 将弧度转换为度
        return Math.toDegrees(theta_rad);
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值