Java 已知两个坐标经纬度以及两个坐标方向(应该是方向角度数)求两点相交得交点经纬度
时间: 2023-07-16 07:16:33 浏览: 136
这个问题需要用到一些基本的地理学知识和公式,以下是一个简单的 Java 实现:
```java
public class IntersectionPoint {
// 地球半径,单位为米
private static final double EARTH_RADIUS = 6378137;
// 计算两点间的距离,返回单位为米
private static double distance(double lat1, double lng1, double lat2, double lng2) {
double radLat1 = Math.toRadians(lat1);
double radLat2 = Math.toRadians(lat2);
double a = radLat1 - radLat2;
double b = Math.toRadians(lng1) - Math.toRadians(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)));
return s * EARTH_RADIUS;
}
// 计算方向角,返回值为角度数
private static double direction(double lat1, double lng1, double lat2, double lng2) {
double dx = Math.cos(Math.toRadians(lat2)) * Math.sin(Math.toRadians(lng2 - lng1));
double dy = Math.cos(Math.toRadians(lat1)) * Math.sin(Math.toRadians(lat2)) -
Math.sin(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.cos(Math.toRadians(lng2 - lng1));
double angle = Math.atan2(dx, dy);
return (Math.toDegrees(angle) + 360) % 360;
}
// 计算交点坐标,返回值为经纬度数组
private static double[] intersection(double lat1, double lng1, double dir1,
double lat2, double lng2, double dir2) {
double d = distance(lat1, lng1, lat2, lng2);
double radDir1 = Math.toRadians(dir1);
double radDir2 = Math.toRadians(dir2);
double radLat1 = Math.toRadians(lat1);
double radLat2 = Math.toRadians(lat2);
double radLng1 = Math.toRadians(lng1);
double radLng2 = Math.toRadians(lng2);
double x = Math.sin(radDir1) * Math.cos(radDir2) * Math.sin(radLat2 - radLat1);
double y = Math.sin(radDir1) * Math.cos(radDir2) * Math.cos(radLat2 - radLat1) * Math.sin(radLng2 - radLng1);
double z = Math.cos(radDir1) * Math.sin(radDir2) * Math.sin(radLat2 - radLat1) * Math.cos(radLng2 - radLng1);
double a = Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2);
double b = 2 * EARTH_RADIUS * (x * (radLng2 - radLng1) + y * (radLat2 - radLat1) + z * (Math.PI / 2 - radLat2));
double c = Math.pow(EARTH_RADIUS, 2) * Math.pow(d, 2) - Math.pow(x * EARTH_RADIUS * (radLng2 - radLng1), 2) -
Math.pow(y * EARTH_RADIUS * (radLat2 - radLat1), 2) - Math.pow(z * EARTH_RADIUS * (Math.PI / 2 - radLat2), 2);
double delta = Math.pow(b, 2) - 4 * a * c;
if (delta < 0) {
return null;
}
double t = (-b + Math.sqrt(delta)) / (2 * a);
double lat = Math.asin(Math.sin(radLat1) * Math.cos(t) + Math.cos(radLat1) * Math.sin(t) * Math.cos(radDir1));
double lng = radLng1 + Math.atan2(Math.sin(t) * Math.sin(radDir1) * Math.sin(radLat1),
Math.cos(t) - Math.sin(radLat1) * Math.sin(lat));
return new double[]{Math.toDegrees(lat), Math.toDegrees(lng)};
}
public static void main(String[] args) {
double lat1 = 39.9042;
double lng1 = 116.4074;
double dir1 = 30;
double lat2 = 31.2304;
double lng2 = 121.4737;
double dir2 = 220;
double[] intersection = intersection(lat1, lng1, dir1, lat2, lng2, dir2);
if (intersection == null) {
System.out.println("No intersection point.");
} else {
System.out.printf("Intersection point: (%f, %f)\n", intersection[0], intersection[1]);
}
}
}
```
以上代码假设输入的经纬度为 WGS84 坐标系下的经纬度,计算结果也是 WGS84 坐标系下的经纬度。如果需要用其他坐标系,需要进行转换。
阅读全文