package com.ruoyi.common.utils; import org.gavaghan.geodesy.Ellipsoid; import org.gavaghan.geodesy.GeodeticCalculator; import org.gavaghan.geodesy.GeodeticCurve; import org.gavaghan.geodesy.GlobalCoordinates; /** * 地图工具类 */ public class MapTools { private static final double EARTH_RADIUS = 6371000; // 地球半径,单位:米 /** * 计算经纬度 */ public static double calculate(String lon1, String lat1, String lon2, String lat2) { double meter1 = 0D; try { // //121.717594,31.12055 121.817629,31.090867 GlobalCoordinates source = new GlobalCoordinates(Double.parseDouble(lat1), Double.parseDouble(lon1)); GlobalCoordinates target = new GlobalCoordinates(Double.parseDouble(lat2), Double.parseDouble(lon2)); meter1 = getDistanceMeter(source, target, Ellipsoid.Sphere); return meter1; } catch (Exception e) { return meter1; } } // public static double calculate(String lon1, String lat1, String lon2, String lat2) { // // double distance = getDistance(Double.parseDouble(lat1), Double.parseDouble(lon1), Double.parseDouble(lat2), Double.parseDouble(lon2)); // return distance; // // } private static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid) { //创建GeodeticCalculator,调用计算方法,传入坐标系、经纬度用于计算距离 GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo); return geoCurve.getEllipsoidalDistance(); } public static double getDistance(double lat1, double lon1, double lat2, double lon2) { double lat1Rad = toRadians(lat1); double lon1Rad = toRadians(lon1); double lat2Rad = toRadians(lat2); double lon2Rad = toRadians(lon2); double a = Math.sin((lat2Rad - lat1Rad) / 2) * Math.sin((lat2Rad - lat1Rad) / 2) + Math.cos(lat1Rad) * Math.cos(lat2Rad) * Math.sin((lon2Rad - lon1Rad) / 2) * Math.sin((lon2Rad - lon1Rad) / 2); double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); return EARTH_RADIUS * c; } private static double toRadians(double degree) { return degree * Math.PI / 180; } }