你作为Java后端工程师完成以下代码快递有经纬度的计算
package com.example.demo;
import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入快递起点经度:"); double startLongitude = scanner.nextDouble(); System.out.print("请输入快递起点纬度:"); double startLatitude = scanner.nextDouble(); System.out.print("请输入快递终点经度:"); double endLongitude = scanner.nextDouble(); System.out.print("请输入快递终点纬度:"); double endLatitude = scanner.nextDouble(); double distance = getDistance(startLongitude, startLatitude, endLongitude, endLatitude); System.out.println("两点间距离为:" + distance + "米"); }
public static double getDistance(double startLongitude, double startLatitude, double endLongitude, double endLatitude) {
double earthRadius = 6371000;// 地球半径,单位为米
double radStartLongitude = Math.toRadians(startLongitude);
double radEndLongitude = Math.toRadians(endLongitude);
double radStartLatitude = Math.toRadians(startLatitude);
double radEndLatitude = Math.toRadians(endLatitude);
double a = Math.pow(Math.sin((radStartLatitude - radEndLatitude) / 2), 2) + Math.cos(radStartLatitude) * Math.cos(radEndLatitude) * Math.pow(Math.sin((radStartLongitude - radEndLongitude) / 2), 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double distance = earthRadius * c;
return distance;
}
}
原文地址: https://www.cveoy.top/t/topic/bhrc 著作权归作者所有。请勿转载和采集!