通过经纬度计算两点之间的距离

1. 相关依赖

xml 复制代码
	<!--计算距离-->
	<dependency>
		<groupId>org.gavaghan</groupId>
		<artifactId>geodesy</artifactId>
		<version>1.1.3</version>
	</dependency>

2.具体的工具类的实现

java 复制代码
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;

import java.math.BigDecimal;

public class CalculatedDistanceUtils {


    /**
     * @param sourceLongitude 来源经度(用户)
     * @param sourceLatitude 来源纬度(用户)
     * @param targetLongitude 目标经度
     * @param targetLatitude 目标纬度
     * @return 米
     */
    public static double getDistanceMeter(BigDecimal sourceLongitude, BigDecimal sourceLatitude, BigDecimal targetLongitude, BigDecimal targetLatitude){

        //可能会出现用户经纬度为空的情况,那就返回个-1
        if (sourceLongitude == null || sourceLatitude == null){
            return -1;
        }

        GlobalCoordinates source = new GlobalCoordinates(sourceLatitude.doubleValue(), sourceLongitude.doubleValue());
        GlobalCoordinates target = new GlobalCoordinates(targetLatitude.doubleValue(), targetLongitude.doubleValue());

        return getDistanceMeter(source, target, Ellipsoid.Sphere);
    }


    public static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid){

        //创建GeodeticCalculator,调用计算方法,传入坐标系、经纬度用于计算距离
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);

        return geoCurve.getEllipsoidalDistance();
    }

}
相关推荐
困知勉行19852 分钟前
springboot整合redis
java·spring boot·redis
wuk9986 分钟前
基于MATLAB实现栅格地图全覆盖移动路径规划
开发语言·matlab
颜淡慕潇6 分钟前
深度解析官方 Spring Boot 稳定版本及 JDK 配套策略
java·后端·架构
中年程序员一枚17 分钟前
Springboot报错Template not found For name “java/lang/Object_toString.sql
java·spring boot·python
幽络源小助理26 分钟前
PHP虚拟商品自动发卡系统源码 – 支持文章付费阅读与自动发货
开发语言·php
故事不长丨30 分钟前
C#集合:解锁高效数据管理的秘密武器
开发语言·windows·c#·wpf·集合·winfrom·字典
知识分享小能手33 分钟前
Ubuntu入门学习教程,从入门到精通,Ubuntu 22.04中的Java与Android开发环境 (20)
java·学习·ubuntu
南屿欣风41 分钟前
FeignClient 踩坑:@FeignClient 同时配 value 和 url 的 “无效服务名” 问题
java
superman超哥1 小时前
Rust 内部可变性模式:突破借用规则的受控机制
开发语言·后端·rust·rust内部可变性·借用规则·受控机制
豆沙沙包?1 小时前
2026年--Lc329-735. 小行星碰撞(栈)--java版
java·开发语言