Pu Zhibing
2025-03-28 e1ea85f4d18916efcd568b9b886a20184c2daeb2
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.ruoyi.system.util;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.utils.HttpUtils;
import com.ruoyi.common.core.utils.StringUtils;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 高德地图的地理编码工具类
 */
public class GDMapGeocodingUtil {
    
    private static String key = "d963b3048d5662b970de1bd41ebdac5e";
    
    
    /**
     * 将行政区域名称转化为坐标
     *
     * @param province
     * @param city
     * @param county
     * @param address
     * @return
     */
    public static Map<String, Object> geocoding(String province, String city, String county, String address) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isEmpty(province)) {
            map.put("status", -1);
            map.put("data", "省不能为空");
            return map;
        }
        if ((StringUtils.isEmpty(city) && StringUtils.isNotEmpty(county)) || (StringUtils.isEmpty(city) && StringUtils.isNotEmpty(address))) {
            map.put("status", -1);
            map.put("data", "市不能为空");
            return map;
        }
        if ((StringUtils.isEmpty(county) && StringUtils.isNotEmpty(address))) {
            map.put("status", -1);
            map.put("data", "县/区不能为空");
            return map;
        }
        
        String url = "https://restapi.amap.com/v3/geocode/geo?key=" + key + "&output=JSON";
        url += "&address=" + province + (StringUtils.isNotEmpty(city) ? city : "") + (StringUtils.isNotEmpty(county) ? county : "") + (StringUtils.isNotEmpty(address) ? address : "");
        String forObject = HttpUtils.sendGet(url);
        JSONObject jsonObject = JSON.parseObject(forObject);
        String status = jsonObject.getString("status");
        List<String> list = new ArrayList<>();
 
//        gdInterfaceService.saveData("https://restapi.amap.com/v3/geocode/geo", "行政区域转经纬度");
        
        if (status.equals("1")) {
            JSONArray geocodes = jsonObject.getJSONArray("geocodes");
            for (int i = 0; i < geocodes.size(); i++) {
                String location = geocodes.getJSONObject(i).getString("location");
                list.add(location);
            }
        }
        map.put("status", 0);
        map.put("data", list);
        return map;
    }
    
    
    public static Map<String, Object> geocoding(String address) {
        Map<String, Object> map = new HashMap<>();
        String url = "https://restapi.amap.com/v3/geocode/geo?key=" + key + "&output=JSON&address=" + address;
        String forObject = HttpUtils.sendGet(url);
        JSONObject jsonObject = JSON.parseObject(forObject);
        String status = jsonObject.getString("status");
        List<String> list = new ArrayList<>();
 
//        gdInterfaceService.saveData("https://restapi.amap.com/v3/geocode/geo", "行政区域转经纬度");
        
        if (status.equals("1")) {
            JSONArray geocodes = jsonObject.getJSONArray("geocodes");
            for (int i = 0; i < geocodes.size(); i++) {
                String location = geocodes.getJSONObject(i).getString("location");
                list.add(location);
            }
        }
        map.put("status", 0);
        map.put("data", list);
        return map;
    }
    
    
    /**
     * 根据经纬度获取行政区域信息
     *
     * @param lon
     * @param lan
     * @return
     * @throws Exception
     */
    public static Map<String, String> geocode(String lon, String lan) throws Exception {
        String url = "https://restapi.amap.com/v3/geocode/regeo?key=" + key + "&location=" + lon + "," + lan;
        String forObject = HttpUtils.sendGet(url);
        JSONObject jsonObject = JSON.parseObject(forObject);
        Map<String, String> map = new HashMap<>();
 
//        gdInterfaceService.saveData("https://restapi.amap.com/v3/geocode/regeo", "经纬度转行政区域");
        
        if (jsonObject.getString("status").equals("1")) {
            JSONObject regeocode = jsonObject.getJSONObject("regeocode");
            JSONObject addressComponent = regeocode.getJSONObject("addressComponent");
            String address = regeocode.getString("formatted_address");
            map.put("address", address);
            String code = addressComponent.getString("adcode");
            String province = addressComponent.getString("province");
            String city = addressComponent.getString("city");
            String district = addressComponent.getString("district");
            map.put("province", province);
            map.put("provinceCode", code.substring(0, 2) + "0000");
            map.put("city", city);
            map.put("cityCode", code.substring(0, 4) + "00");
            map.put("district", district);
            map.put("districtCode", code);
        }
        return map;
    }
}