/**
* 描述:TODO::添加描述
*
* @author Jack Que
* @version 1.0.0 -SNAPSHOT
* @created 2022 -05-29 11:57:34
* @create 2022 -05-29 11:50:08
* @since 1.0.0 -SNAPSHOT
*/
public class LocationUtils {
/**
* 赤道半径
*/
private static double EARTH_RADIUS = 6378.137;
/**
* 描述 create.
*
* @param d the d
* @return the double
* @author Jack Que
* @created 2022 -05-29 11:57:34 Rad double.
*/
private static double rad(double d) {
return d * Math.PI / 180.0;
}
/**
* Description : 通过经纬度获取距离(单位:米)
* Group :
*
* @param originLon 出发点经度
* @param originLat 出发点纬度
* @param destinationLon 目的地经度
* @param destinationLat 目的地纬度
* @return double distance
* @author Jack Que
* @created 2022 -05-29 11:57:34
*/
public static Integer getDistance(String originLon, String originLat, String destinationLon, String destinationLat) {
try {
double radLat1 = rad(Double.parseDouble(originLat));
double radLat2 = rad(Double.parseDouble(destinationLat));
double a = radLat1 - radLat2;
double b = rad(Double.parseDouble(originLon)) - rad(Double.parseDouble(destinationLon));
double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
+ Math.cos(radLat1) * Math.cos(radLat2)
* Math.pow(Math.sin(b / 2), 2)));
s = s * EARTH_RADIUS;
// 保留两位小数
s = Math.round(s * 100d) / 100d;
s = s * 1000;
return new Double(s).intValue();
}catch (Exception e){
System.out.println("计算距离出错:"+e.getMessage());
}
return null;
}
/**
* 描述 map通过经纬度获取距离(单位:米)
*
* @param map the map
* @param originLon 出发点经度
* @param originLat 出发点纬度
* @author Jack Que
* @created 2022 -05-29 11:57:34 Get distance map.
*/
public static void getDistanceMap(Map<String,Object> map,String originLon, String originLat){
if (StringUtils.isEmpty(map.get("LAT")) || StringUtils.isEmpty(map.get("LON"))){
System.out.println("经纬度为空!");
return;
}
Integer distance = getDistance(originLon, originLat, map.get("LON").toString(), map.get("LAT").toString());
map.put("DISTANCE",distance);
}
public static void getDistanceList(List<Map<String,Object>> list, String originLon, String originLat){
if (CollectionUtils.isEmpty(list)){
System.out.println("列表为空");
return;
}
list.forEach(item->{
if (!CollectionUtils.isEmpty(item)){
getDistanceMap(item,originLon,originLat);
}
});
}
}
引用:https://blog.csdn.net/weixin_43866048/article/details/119349603