package com.ruoyi.web.controller.tool; import okhttp3.*; import com.alibaba.fastjson.JSONObject; import java.io.IOException; public class HighPrecisionCoordinateTransformUtil { private static final String GAODE_API_KEY = "e700a58329a38b2d8980790d9b1b5b06"; // 需要申请高德地图API Key private static final String GAODE_CONVERT_URL = "https://restapi.amap.com/v3/assistant/coordinate/convert"; private static final OkHttpClient client = new OkHttpClient(); /** * 使用高德地图API进行高精度坐标转换 WGS84 -> GCJ-02 * @param wgLat WGS84纬度 * @param wgLon WGS84经度 * @return 转换后的GCJ-02坐标 [纬度, 经度] */ public static double[] wgs84ToGcj02ViaGaode(double wgLat, double wgLon) { String url = GAODE_CONVERT_URL + "?locations=" + wgLon + "," + wgLat + "&coordsys=gps" + "&output=json" + "&key=" + GAODE_API_KEY; Request request = new Request.Builder() .url(url) .get() .build(); try { Response response = client.newCall(request).execute(); if (response.isSuccessful() && response.body() != null) { String responseBody = response.body().string(); JSONObject jsonObject = JSONObject.parseObject(responseBody); if ("1".equals(jsonObject.getString("status"))) { String[] locations = jsonObject.getString("locations").split(","); // 返回格式: [纬度, 经度] return new double[]{ Double.parseDouble(locations[1]), // 纬度 Double.parseDouble(locations[0]) // 经度 }; } } } catch (IOException e) { e.printStackTrace(); } // API调用失败时回退到原来的算法 return CoordinateTransform.wgs84ToGcj02(wgLat, wgLon); } /** * 测试方法 */ }