| | |
| | | /** 距离矩阵API */ |
| | | public static final String MATRIX_URL = "https://apis.map.qq.com/ws/distance/v1/matrix"; |
| | | |
| | | /** 逆地理解析 */ |
| | | public static final String GEO_URL = "https://apis.map.qq.com/ws/geocoder/v1/"; |
| | | |
| | | /** 支持的模式 */ |
| | | private static final List<String> SUPPORTED_MODES = Arrays.asList("driving", "bicycling"); |
| | | |
| | |
| | | .map(row -> ((JSONObject) row).getJSONArray("elements").getJSONObject(0)) |
| | | .collect(Collectors.toList()); |
| | | } |
| | | |
| | | /** |
| | | * 逆地理解析 - 根据经纬度获取地址信息 |
| | | * |
| | | * @param lat 纬度 |
| | | * @param lng 经度 |
| | | * @return result.ad_info 中的 adcode(区划码)、city(城市)、district(区) 等信息 |
| | | */ |
| | | public static JSONObject reverseGeocode(double lat, double lng) { |
| | | try { |
| | | String url = GEO_URL |
| | | + "?key=" + tencentKey |
| | | + "&location=" + lat + "," + lng; |
| | | |
| | | log.info("腾讯地图逆地理解析请求: location={},{}", lat, lng); |
| | | |
| | | JSONObject json = new Http().build(url) |
| | | .setConnectTimeout(5000) |
| | | .setReadTimeout(10000) |
| | | .get() |
| | | .toJSONObject(); |
| | | |
| | | log.info("腾讯地图逆地理解析响应: {}", json); |
| | | |
| | | if (json.getIntValue("status") != 0) { |
| | | throw new RuntimeException("腾讯地图逆地理解析失败: " + json.getString("message")); |
| | | } |
| | | |
| | | return json.getJSONObject("result"); |
| | | } catch (IOException e) { |
| | | log.error("腾讯地图逆地理解析异常", e); |
| | | throw new RuntimeException("腾讯地图逆地理解析异常", e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 判断两个经纬度是否在同一个城市 |
| | | * |
| | | * @param lat1 第一个点纬度 |
| | | * @param lng1 第一个点经度 |
| | | * @param lat2 第二个点纬度 |
| | | * @param lng2 第二个点经度 |
| | | * @return true=同城,false=不同城 |
| | | */ |
| | | public static boolean isSameCity(double lat1, double lng1, double lat2, double lng2) { |
| | | JSONObject result1 = reverseGeocode(lat1, lng1); |
| | | JSONObject result2 = reverseGeocode(lat2, lng2); |
| | | |
| | | String city1 = result1.getJSONObject("ad_info").getString("city"); |
| | | String city2 = result2.getJSONObject("ad_info").getString("city"); |
| | | |
| | | log.info("判断同城: ({},{}) => city={}, ({},{}) => city={}", lat1, lng1, city1, lat2, lng2, city2); |
| | | |
| | | return city1 != null && city1.equals(city2); |
| | | } |
| | | } |