经纬度 角点计算

This commit is contained in:
wangw 2024-04-12 09:56:32 +08:00
parent f8c53d3548
commit 7d3fe9deb7
3 changed files with 55 additions and 26 deletions

View File

@ -78,7 +78,7 @@ public class LoginFilter implements Filter {
// 判断用户TOKEN是否存在
String token = request.getHeader("token");
if (StringUtils.isBlank(token)) {
Result result = new Result(CodeEnum.TOKEN_EXEIST);
Result result = new Result(CodeEnum.TOKEN_EXPIRED);
String jsonString = JSONObject.toJSONString(result);
JSONObject jsonObject = JSONObject.parseObject(jsonString, JSONObject.class);
response.getWriter().print(jsonObject);

View File

@ -99,6 +99,7 @@ public class HomePageService {
List<TbPlatformDict> tbPlatformDicts = platformDictMapper.queryByIdList(shopTagIds);
homeVO.setShopTag(JSONUtil.parseListTNewList(tbPlatformDicts, TagVo.class));
}
}
}
for (TbProduct tbProduct : product.get()) {

View File

@ -9,30 +9,18 @@ import static java.lang.Math.sin;
public class LocationUtils {
public static String district(String keywords) {
Map<String, String> param=new HashMap<>();
Map<String, String> param = new HashMap<>();
//超掌柜生活-用户端
param.put("key","7a7f2e4790ea222660a027352ee3af39");
param.put("keywords",keywords);
param.put("subdistrict","2");
param.put("extensions","base");
param.put("key", "7a7f2e4790ea222660a027352ee3af39");
param.put("keywords", keywords);
param.put("subdistrict", "2");
param.put("extensions", "base");
String s = HttpClientUtil.doGet("https://restapi.amap.com/v3/config/district", param);
return s;
}
/**
* 将角度转化为弧度
*/
public static double radians(double d) {
return d * Math.PI / 180.0;
}
/**
* 根据两点经纬度坐标计算直线距离
* <p>
* S = 2arcsinsin²(a/2)+cos(lat1)*cos(lat2)*sin²(b/2)*6378.137
* <p>
* 1. lng1 lat1 表示A点经纬度lng2 lat2 表示B点经纬度<br>
* 2. a=lat1 lat2 为两点纬度之差 b=lng1 -lng2 为两点经度之差<br>
* 3. 6378.137为地球赤道半径单位为千米
*
* @param lng1 点1经度
* @param lat1 点1纬度
@ -43,7 +31,7 @@ public class LocationUtils {
*/
public static double getDistanceFrom2LngLat(double lng1, double lat1, double lng2, double lat2) {
//将角度转化为弧度
double radLng1 = radians(lng1);
double radLng1 = radians(lng1);//d * Math.PI / 180.0;
double radLat1 = radians(lat1);
double radLng2 = radians(lng2);
double radLat2 = radians(lat2);
@ -54,16 +42,56 @@ public class LocationUtils {
return 2 * asin(sqrt(sin(a / 2) * sin(a / 2) + cos(radLat1) * cos(radLat2) * sin(b / 2) * sin(b / 2))) * 6378.137;
}
/**
* @param longitude 经度
* @param latitude 纬度
* @param distanceInKm 范围千米
* return Map<String, double[]> stringMap
* 右顶点 double[] leftTopPoints = stringMap.get("rightTopPoint");
* 左顶点 double[] leftBottomPoints = stringMap.get("leftBottomPoint");
*/
public static Map<String, double[]> returnLLSquarePoint(double longitude, double latitude, double distanceInKm) {
Map<String, double[]> squareMap = new HashMap<>();
// 地球平均半径单位千米
double earthRadius = 6378.137;
// 将距离转换为弧度
double radialDistance = distanceInKm / earthRadius;
// 计算纬度上变化对应的角度
double dLatitude = Math.toDegrees(2 * Math.asin(Math.sin(radialDistance / 2) / Math.cos(Math.toRadians(latitude))));
// 计算经度上变化对应的角度这里需要考虑纬度对距离的影响
// 使用Haversine公式来估算在给定纬度上距离对应的经度变化
double estimatedLongitudeDistance = getDistanceFrom2LngLat(latitude, longitude, latitude, longitude + 0.01); // 假设0.01度经度变化对应的距离
double dLongitude = distanceInKm / estimatedLongitudeDistance * 0.0192736; // 根据比例计算经度变化
// 正方形四个顶点的坐标
double[] rightTopPoint = {latitude + dLatitude / 2, longitude + dLongitude / 2};
double[] leftBottomPoint = {latitude - dLatitude / 2, longitude - dLongitude / 2};
squareMap.put("rightTopPoint", rightTopPoint);
squareMap.put("leftBottomPoint", leftBottomPoint);
// 打印结果
// System.out.println("rightTop" + rightTopPoint[0] + "======" + rightTopPoint[1]);
// System.out.println("leftBottom" + leftBottomPoint[0] + "======" + leftBottomPoint[1]);
return squareMap;
}
/**
* 将角度转化为弧度
*/
public static double radians(double d) {
return d * Math.PI / 180.0;
}
// public static void main(String[] args) {
// // 示例经纬度坐标
// double lat1 = 108.954398;
// double lon1 = 34.308687;
// Map<String, double[]> stringMap = returnLLSquarePoint(108.954398, 34.308687, 1);
// double[] leftTopPoints = stringMap.get("rightTopPoint");
// double[] leftBottomPoints = stringMap.get("leftBottomPoint");
//
// double lat2 = 108.953555;
// double lon2 = 34.276169;
// double lat2 = 108.954398;
// double lon2 = 34.308687;
//
// // 计算距离
// double distance = getDistanceFrom2LngLat(lat1, lon1, lat2, lon2);
// System.out.println("Distance between the two points is: " + distance + " km");
// double distance1 = getDistanceFrom2LngLat(leftTopPoints[1], leftTopPoints[0], lat2, lon2);
// double distance2 = getDistanceFrom2LngLat(leftBottomPoints[1], leftBottomPoints[0], lat2, lon2);
// System.out.println("Distance between the two points is: " + distance1 + " km");
// System.out.println("Distance between the two points is: " + distance2 + " km");
// }
}