无关风月
2 天以前 1fd6775420288be4dce934fe845328e9501bf023
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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);
    }
    
    /**
     * 测试方法
     */
 
}