cjl
2023-10-27 b62adf1d73b62711180a5332d7c2c71d2c0a1cfb
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
package com.moral.api.utils;
 
import com.moral.api.entity.GeoCoordinate;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * @program: screen
 * @description: 计算多个地点的中心点
 * @author: lizijie
 * @create: 2021-12-09 16:37
 **/
public class GetCenterPointFromListOfCoordinates {
 
    /**
     *  根据输入的地点坐标计算中心点
     * @param geoCoordinateList
     * @return
     */
    public static GeoCoordinate getCenterPoint(List<GeoCoordinate> geoCoordinateList) {
        int total = geoCoordinateList.size();
        double X = 0, Y = 0, Z = 0;
        for (GeoCoordinate g : geoCoordinateList) {
            double lat, lon, x, y, z;
            lat = g.getLatitude() * Math.PI / 180;
            lon = g.getLongitude() * Math.PI / 180;
            x = Math.cos(lat) * Math.cos(lon);
            y = Math.cos(lat) * Math.sin(lon);
            z = Math.sin(lat);
            X += x;
            Y += y;
            Z += z;
        }
 
        X = X / total;
        Y = Y / total;
        Z = Z / total;
        double Lon = Math.atan2(Y, X);
        double Hyp = Math.sqrt(X * X + Y * Y);
        double Lat = Math.atan2(Z, Hyp);
        return new GeoCoordinate(Lat * 180 / Math.PI, Lon * 180 / Math.PI);
    }
 
    /**
     * 根据输入的地点坐标计算中心点(适用于400km以下的场合)
     * @param geoCoordinateList
     * @return
     */
    public static GeoCoordinate getCenterPoint400(List<GeoCoordinate> geoCoordinateList) {
        // 以下为简化方法(400km以内)
        int total = geoCoordinateList.size();
        double lat = 0, lon = 0;
        for (GeoCoordinate g : geoCoordinateList) {
            lat += g.getLatitude() * Math.PI / 180;
            lon += g.getLongitude() * Math.PI / 180;
        }
        lat /= total;
        lon /= total;
        return new GeoCoordinate(lat * 180 / Math.PI, lon * 180 / Math.PI);
    }
 
    public static void main(String[] args) {
        List geoCoordinateList = new ArrayList();
        GeoCoordinate g = new GeoCoordinate();
        g.setLongitude(112.977324);
        g.setLatitude(28.178376);
        GeoCoordinate g2 = new GeoCoordinate();
        g2.setLongitude(112.975782);
        g2.setLatitude(28.172258);
        geoCoordinateList.add(g);
        geoCoordinateList.add(g2);
 
        GeoCoordinate  re = GetCenterPointFromListOfCoordinates.getCenterPoint(geoCoordinateList);
        System.out.println(re.getLongitude() +"   "+ re.getLatitude());
    }
 
 
}