puzhibing
2026-04-10 ed838e62602c0a63a64ff4bd144a0d16cc9a7cf8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.xiaopeng.automaticPilot.util;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * GPS路径生成工具
 */
public class GpsPathGenerator {
 
    /**
     * 地球半径(米)
     */
    private static final double EARTH_RADIUS = 6371000;
 
    /**
     * 简单GPS坐标点
     */
    public static class GpsPoint {
        public final double longitude;
        public final double latitude;
 
        public GpsPoint(double longitude, double latitude) {
            this.longitude = longitude;
            this.latitude = latitude;
        }
    }
 
    /**
     * 生成从起点到终点的GPS路径
     *
     * @param startLongitude  起点经度
     * @param startLatitude   起点纬度
     * @param endLongitude    终点经度
     * @param endLatitude     终点纬度
     * @param intervalSeconds 间隔秒数(每隔多少秒一个点)
     * @param speed           速度(米/秒),默认10.5米/秒
     * @return GPS坐标点列表
     */
    public static List<GpsPoint> generatePath(double startLongitude, double startLatitude,
                                               double endLongitude, double endLatitude,
                                               int intervalSeconds, double speed) {
        List<GpsPoint> pathPoints = new ArrayList<>();
 
        // 计算总距离(米)
        double totalDistance = calculateDistance(startLatitude, startLongitude, endLatitude, endLongitude);
 
        // 计算每个间隔移动的距离
        double distancePerInterval = speed * intervalSeconds;
 
        // 计算需要的点数
        int pointCount = (int) Math.ceil(totalDistance / distancePerInterval) + 1;
 
        // 生成路径点
        for (int i = 0; i < pointCount; i++) {
            double ratio = i * distancePerInterval / totalDistance;
            if (ratio > 1.0) {
                ratio = 1.0;
            }
 
            // 线性插值计算中间点
            double longitude = startLongitude + (endLongitude - startLongitude) * ratio;
            double latitude = startLatitude + (endLatitude - startLatitude) * ratio;
 
            pathPoints.add(new GpsPoint(longitude, latitude));
        }
 
        return pathPoints;
    }
 
 
    /**
     * 使用Haversine公式计算两个GPS坐标之间的距离(米)
     *
     * @param lat1 起点纬度
     * @param lon1 起点经度
     * @param lat2 终点纬度
     * @param lon2 终点经度
     * @return 距离(米)
     */
    public static double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
        double lat1Rad = Math.toRadians(lat1);
        double lat2Rad = Math.toRadians(lat2);
        double deltaLat = Math.toRadians(lat2 - lat1);
        double deltaLon = Math.toRadians(lon2 - lon1);
 
        double a = Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2) +
                Math.cos(lat1Rad) * Math.cos(lat2Rad) *
                        Math.sin(deltaLon / 2) * Math.sin(deltaLon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        return EARTH_RADIUS * c;
    }
 
    /**
     * 计算两个点之间的方位角(度)
     *
     * @param lat1 起点纬度
     * @param lon1 起点经度
     * @param lat2 终点纬度
     * @param lon2 终点经度
     * @return 方位角(0-360度)
     */
    public static int calculateCourse(double lat1, double lon1, double lat2, double lon2) {
        double lat1Rad = Math.toRadians(lat1);
        double lat2Rad = Math.toRadians(lat2);
        double deltaLon = Math.toRadians(lon2 - lon1);
 
        double y = Math.sin(deltaLon) * Math.cos(lat2Rad);
        double x = Math.cos(lat1Rad) * Math.sin(lat2Rad) -
                Math.sin(lat1Rad) * Math.cos(lat2Rad) * Math.cos(deltaLon);
 
        double bearing = Math.toDegrees(Math.atan2(y, x));
 
        // 转换为0-360度
        return (int) ((bearing + 360) % 360);
    }
}