JAVA獲取多個經緯度的中心點


 

 

 

import java.util.LinkedList;

public class Test1 {

    /**
     * 位置實體類,根據自己的來即可
     */
    static class Position{

        /**
         * 緯度
         */
        private Double latitude;

        /**
         * 經度
         */
        private Double longitude;

        public Double getLatitude() {
            return latitude;
        }

        public void setLatitude(Double latitude) {
            this.latitude = latitude;
        }

        public Double getLongitude() {
            return longitude;
        }

        public void setLongitude(Double longitude) {
            this.longitude = longitude;
        }

        public Position(Double latitude, Double longitude) {
            this.latitude = latitude;
            this.longitude = longitude;
        }
    }


    /**
     *  取幾個經緯度的中心點
     * @param postionList 經緯度的集合
     */
    public static void getCenterPoint(LinkedList<Position> postionList) {
        int total = postionList.size();
        double X = 0, Y = 0, Z = 0;

        while(!postionList.isEmpty()) {
            Position g = postionList.pollFirst();
            if(g != null) {
                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);
        double longitude = Lon * 180 / Math.PI;
        double latitude = Lat * 180 / Math.PI;
        System.out.println("中心點經度:"+longitude);
        System.out.println("中心點緯度:"+latitude);

    }


    public static void main(String[] args) {
        LinkedList<Position> list=new LinkedList<>();
        list.add(new Position(35.1719916700000040,119.2328202000000200));
        list.add(new Position(35.1319216700000040,119.2925302000000200));
        list.add(new Position(35.1919216700000040,119.2625302000000200));

        getCenterPoint(list);
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM