java经纬度计算工具类(含测试方法)

文章?p>

柯?/h4>

      • 代码

代码

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.math3.util.FastMath;

import java.util.*;

public class DistanceCalculator {

 public static void main(String[] args) {
        List<Activity> activities = Arrays.asList(
                new Activity("目标地址", 22.543903,113.911223) 
        );
        // 当前用户地址
        double userLat = 22.5859635925293; 
        double userLng = 113.86769104003906; 
        List<Activity> sortedActivities = sortActivitiesByDistance(activities, userLat, userLng);
        Map<String,String> seatMap = new HashMap<String,String>();

        for (Activity activity : sortedActivities) {
            double km = DistanceCalculator.calculateDistance(userLat, userLng, activity.getLat(), activity.getLng());
            double m = km * 1000;
            String s = convertToFriendlyUnit(m);
            seatMap.put(activity.getName(),s);
//            System.out.println("Activity: " + activity.getName() + ", Distance: " + DistanceCalculator.calculateDistance(userLat, userLng, activity.getLat(), activity.getLng()) + " km");
        }

        System.out.println(seatMap);


    }


    public static String getDistance(String userPoi,String targetPoi){
        if(StringUtils.isBlank(userPoi)){
            return "附近";
        }else if(StringUtils.isNotBlank(targetPoi)){
            String[] split = targetPoi.split(",");
            if(split.length == 2){
                double lat = Double.parseDouble(split[0]);
                double lng = Double.parseDouble(split[1]);
                String[] userLaction = userPoi.split(",");
                if(userLaction.length == 2){
                    double userLat = Double.parseDouble(userLaction[0]);
                    double userLng = Double.parseDouble(userLaction[1]);
                    return (DistanceCalculator.convertToFriendlyUnit(
                            DistanceCalculator.calculateDistance(userLat,userLng,lat,lng) * 1000
                    ));
                }else{
                    return "附近";
                }
            }else{
                return "附近";
            }
        }else{
            return "附近";
        }
    }

   
    public static String convertToFriendlyUnit(double meters) {
        if (meters >= 1000) {
            return String.format("%.1fkm", meters / 1000);
        } else {
            return String.format("%.1fm", meters);
        }
    }

    public static List<Activity> sortActivitiesByDistance(List<Activity> activities, double userLat, double userLng) {
        Comparator<Activity> distanceComparator = new Comparator<Activity>() {
            @Override
            public int compare(Activity activity1, Activity activity2) {
                double distance1 = DistanceCalculator.calculateDistance(userLat, userLng, activity1.getLat(), activity1.getLng());
                double distance2 = DistanceCalculator.calculateDistance(userLat, userLng, activity2.getLat(), activity2.getLng());
                return Double.compare(distance1, distance2);
            }
        };
        Collections.sort(activities, distanceComparator);
        return activities;
    }

    private static final double R = 6371;

    public static double calculateDistance(double userLat, double userLng, double eventLat, double eventLng) {
        double dLat = toRadians(eventLat - userLat);  
        double dLng = toRadians(eventLng - userLng);  
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(toRadians(userLat)) * Math.cos(toRadians(eventLat)) * Math.sin(dLng / 2) * Math.sin(dLng / 2);  
        double c = 2 * FastMath.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        return R * c; // R is the radius of the Earth (approximate value is 6371 km)  
    }  
  
    private static double toRadians(double degrees) {  
        return degrees * FastMath.PI / 180;  
    }

    public static Double getDistanceMi(String userPoi, String targetPoi) {
        if(StringUtils.isBlank(userPoi)){
            return 9999d;
        }else if(StringUtils.isNotBlank(targetPoi)){
            String[] split = targetPoi.split(",");
            if(split.length == 2){
                double lat = Double.parseDouble(split[0]);
                double lng = Double.parseDouble(split[1]);
                String[] userLaction = userPoi.split(",");
                if(userLaction.length == 2){
                    double userLat = Double.parseDouble(userLaction[0]);
                    double userLng = Double.parseDouble(userLaction[1]);
                    return DistanceCalculator.calculateDistance(userLat,userLng,lat,lng);
                }else{
                    return 9999d;
                }
            }else{
                return 9999d;
            }
        }else{
            return 9999d;
        }
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @ToString
    static class Activity {
        private String name;
        private double lat; // 活动地点的纬度
        private double lng; // 活动地点的经度
        // 构造函数、getter和setter方法...
    }
}