已知多个坐标点 ,计算距离中心坐标点最近点的距离

本文介绍了如何在Java项目中使用Maven坐标引入地理坐标库,并展示了GeoUtils工具类,包括计算两点间的距离、找到中心点以及获取距离中心点最近的点的方法。
摘要由CSDN通过智能技术生成
Maven坐标
        <!--用于计算两点之间的距离-->
        <dependency>
            <groupId>org.gavaghan</groupId>
            <artifactId>geodesy</artifactId>
            <version>1.1.3</version>
        </dependency>

坐标实体
@Data
public class GeoPoint {

    private double latitude;
    private double longitude;

    public GeoPoint(double latitude, double longitude) {
        this.latitude = latitude;
        this.longitude = longitude;
    }
}
工具类

import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;
/**
 * @author : Cookie
 * date : 2024/1/11
 */
public class GeoUtils {
    /**
     * 计算两个经纬度之间的距离
     *
     * @param geoPoint1 第一个经纬度
     * @param geoPoint2 第二个经纬度
     * @return 返回的距离,单位m
     */
    public static double getDistanceMeter(GeoPoint geoPoint1, GeoPoint geoPoint2) {
        GlobalCoordinates gpsFrom = new GlobalCoordinates(geoPoint1.getLongitude(), geoPoint1.getLatitude());
        GlobalCoordinates gpsTo = new GlobalCoordinates(geoPoint2.getLongitude(), geoPoint2.getLatitude());

        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.WGS84, gpsFrom, gpsTo);
        return geoCurve.getEllipsoidalDistance();
    }

    /**
     * 获取距离中心点最近的点
     *
     * @param center 中心点
     * @param points 所有点
     * @return 距离中心点最近的点
     */
    public static GeoPoint getNearestPointToCenter(GeoPoint center, GeoPoint[] points) {

        // 定义最小距离
        double minDistance = Double.MAX_VALUE;
        // 定义最近的点
        GeoPoint nearestPoint = null;
        // 遍历所有点
        for (GeoPoint point : points) {
            // 计算距离
            double distance = GeoUtils.getDistanceMeter(center, point);
            // 如果距离小于最小距离
            if (distance < minDistance) {
                // 更新最小距离和最近的点
                minDistance = distance;
                nearestPoint = point;
            }
        }
        // 返回最近的点
        return nearestPoint;
    }

    /**
     * 获取中心点
     *
     * @param points 所有点
     * @return 中心点
     */
    public static GeoPoint getCenterPoint(GeoPoint[] points) {
        // 计算中心点的纬度
        double latitude = 0;
        for (GeoPoint point : points) {
            latitude += point.getLatitude();
        }
        latitude /= points.length;

        // 计算中心点的经度
        double longitude = 0;
        for (GeoPoint point : points) {
            longitude += point.getLongitude();
        }
        longitude /= points.length;

        // 返回中心点
        return new GeoPoint(latitude, longitude);
    }

}
测试
    public static void main(String[] args) {
        GeoPoint[] points = new GeoPoint[]{
                new GeoPoint(39.939123, 116.395645),
                new GeoPoint(39.929456, 116.393645),
                new GeoPoint(39.959789, 116.397645)};
        // 计算中心点
        GeoPoint center = getCenterPoint(points);
        System.out.println(center);
        // 计算距离中心点最近的点
        GeoPoint nearestPoint = getNearestPointToCenter(center, points);
        System.out.println(nearestPoint);
        // 计算距离 单位m
        double distance = getDistanceMeter(center, nearestPoint);
        System.out.println(distance);
    }
  • 16
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yfs1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值