MapTools.java 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package com.ruoyi.common.utils;
  2. import org.gavaghan.geodesy.Ellipsoid;
  3. import org.gavaghan.geodesy.GeodeticCalculator;
  4. import org.gavaghan.geodesy.GeodeticCurve;
  5. import org.gavaghan.geodesy.GlobalCoordinates;
  6. /**
  7. * 地图工具类
  8. */
  9. public class MapTools {
  10. private static final double EARTH_RADIUS = 6371000; // 地球半径,单位:米
  11. /**
  12. * 计算经纬度
  13. */
  14. public static double calculate(String lon1, String lat1, String lon2, String lat2) {
  15. double meter1 = 0D;
  16. try {
  17. // //121.717594,31.12055 121.817629,31.090867
  18. GlobalCoordinates source = new GlobalCoordinates(Double.parseDouble(lat1), Double.parseDouble(lon1));
  19. GlobalCoordinates target = new GlobalCoordinates(Double.parseDouble(lat2), Double.parseDouble(lon2));
  20. meter1 = getDistanceMeter(source, target, Ellipsoid.Sphere);
  21. return meter1;
  22. } catch (Exception e) {
  23. return meter1;
  24. }
  25. }
  26. // public static double calculate(String lon1, String lat1, String lon2, String lat2) {
  27. //
  28. // double distance = getDistance(Double.parseDouble(lat1), Double.parseDouble(lon1), Double.parseDouble(lat2), Double.parseDouble(lon2));
  29. // return distance;
  30. //
  31. // }
  32. private static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo,
  33. Ellipsoid ellipsoid) {
  34. //创建GeodeticCalculator,调用计算方法,传入坐标系、经纬度用于计算距离
  35. GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);
  36. return geoCurve.getEllipsoidalDistance();
  37. }
  38. public static double getDistance(double lat1, double lon1, double lat2, double lon2) {
  39. double lat1Rad = toRadians(lat1);
  40. double lon1Rad = toRadians(lon1);
  41. double lat2Rad = toRadians(lat2);
  42. double lon2Rad = toRadians(lon2);
  43. double a = Math.sin((lat2Rad - lat1Rad) / 2) *
  44. Math.sin((lat2Rad - lat1Rad) / 2) +
  45. Math.cos(lat1Rad) *
  46. Math.cos(lat2Rad) *
  47. Math.sin((lon2Rad - lon1Rad) / 2) *
  48. Math.sin((lon2Rad - lon1Rad) / 2);
  49. double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  50. return EARTH_RADIUS * c;
  51. }
  52. private static double toRadians(double degree) {
  53. return degree * Math.PI / 180;
  54. }
  55. }