JAVA——输入多个经纬度坐标,找出中心点

具体算法介绍请点击:Python实现方式

package com.gis.launcher;

import com.gis.vo.GeoCoordinate;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fc.w on 2017/12/8.
 */
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<GeoCoordinate> geoCoordinateList = new ArrayList<GeoCoordinate>();
        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());
    }


}




/**
 * 封装经纬度坐标类
 * Created by fc.w on 2017/12/8.
 */
public class GeoCoordinate {

    private double latitude;
    private double longitude;

    public GeoCoordinate() {
    }

    public GeoCoordinate(double latitude, double longitude) {
        this.latitude = latitude;
        this.longitude = 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;
    }
}

参考文献

[1]From stackoverflow
[2]From 详细的算法说明,可以参考

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容