代码
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(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;
}
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;
}
}