puzhibing
2023-11-25 53e7558400dcacecdce70e39ebfe1727740f9296
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package com.dsh.other.util;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dsh.other.util.httpClinet.HttpClientUtil;
import com.dsh.other.util.httpClinet.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 高德地图的地理编码工具类
 */
@Component
public class GDMapGeocodingUtil {
 
    private String key = "fb131ad2dbfb3f39d7d37d244b92aa2d";
 
    /**
     * 将行政区域名称转化为坐标
     *
     * @param province
     * @param city
     * @param county
     * @param address
     * @return
     */
    public Map<String, Object> geocoding(String province, String city, String county, String address) throws Exception {
        Map<String, Object> map = new HashMap<>();
        if (ToolUtil.isEmpty(province)) {
            map.put("status", -1);
            map.put("data", "省不能为空");
            return map;
        }
        if ((ToolUtil.isEmpty(city) && ToolUtil.isNotEmpty(county)) || (ToolUtil.isEmpty(city) && ToolUtil.isNotEmpty(address))) {
            map.put("status", -1);
            map.put("data", "市不能为空");
            return map;
        }
        if ((ToolUtil.isEmpty(county) && ToolUtil.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 + (ToolUtil.isNotEmpty(city) ? city : "") + (ToolUtil.isNotEmpty(county) ? county : "") + (ToolUtil.isNotEmpty(address) ? address : "");
        HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", url, null, null, "json");
        JSONObject jsonObject = JSON.parseObject(httpResult.getData());
        String status = jsonObject.getString("status");
        List<String> list = new ArrayList<>();
 
        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 Map<String, Object> geocoding(String address) throws Exception {
        Map<String, Object> map = new HashMap<>();
        String url = "https://restapi.amap.com/v3/geocode/geo?key=" + key + "&output=JSON&address=" + address;
        HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", url, null, null, "json");
        JSONObject jsonObject = JSON.parseObject(httpResult.getData());
        String status = jsonObject.getString("status");
        List<String> list = new ArrayList<>();
 
        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 Map<String, String> geocode(String lon, String lan) throws Exception {
        String url = "https://restapi.amap.com/v3/geocode/regeo?key=" + key + "&location=" + lon + "," + lan;
        HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", url, null, null, "json");
        JSONObject jsonObject = JSON.parseObject(httpResult.getData());
        Map<String, String> map = new HashMap<>();
 
 
        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;
    }
 
    @Autowired
    private GDMapGeocodingUtil gdMapGeocodingUtil;
 
    public Map<String, String> geocode1(String lon, String lan) throws Exception {
        String url = "https://restapi.amap.com/v3/geocode/regeo?key=" + key + "&location=" + lon + "," + lan;
        HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", url, null, null, "json");
        JSONObject jsonObject = JSON.parseObject(httpResult.getData());
        Map<String, String> map = new HashMap<>();
 
        Map<String, String> geocode = gdMapGeocodingUtil.geocode(lon, lan);
        String provinceCode = geocode.get("provinceCode");
        String cityCode = geocode.get("cityCode");
        map.put("cityCode", cityCode);
 
//        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;
    }
 
 
    /**
     * 坐标转换
     *
     * @param locations 经度和纬度用","分割,经度在前,纬度在后,经纬度小数点后不得超过6位。多个坐标对之间用”|”进行分隔最多支持40对坐标。
     * @param coordsys  可选值:gps;mapbar;baidu;autonavi(不进行转换)
     * @return
     * @throws Exception
     */
    public Map<String, String> convert(String locations, String coordsys) throws Exception {
        String url = "https://restapi.amap.com/v3/assistant/coordinate/convert?locations=" + locations + "&coordsys=" + coordsys + "&output=json&key=" + key;
        HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", url, null, null, "json");
        JSONObject jsonObject = JSON.parseObject(httpResult.getData());
        Map<String, String> map = new HashMap<>();
        if ("1".equals(jsonObject.getString("status"))) {
            map.put("code", jsonObject.getString("infocode"));//"10000"
            map.put("info", jsonObject.getString("info"));//status为0时,info返回错误原;否则返回“OK”。
            map.put("locations", jsonObject.getString("locations").split(";")[0]);//转换之后的坐标。若有多个坐标,则用 “;”进行区分和间隔
        } else {
            map.put("code", jsonObject.getString("infocode"));
            map.put("info", jsonObject.getString("info"));//status为0时,info返回错误原;否则返回“OK”。
        }
        return map;
    }
 
 
    /**
     * 功能描述: 根据两个定位点的经纬度算出两点间的距离(米/m)
     *
     * @param startLonLat 起始经纬度
     * @param endLonLat   结束经纬度(目标经纬度)
     * @return java.lang.Long 两个定位点之间的距离
     */
    public Long getDistance(String startLonLat, String endLonLat) {
        try {
            // 返回起始地startAddr与目的地endAddr之间的距离,单位:米
            Long result = new Long(0);
            String queryUrl =
                    "http://restapi.amap.com/v3/distance?key=" + key + "&origins=" + startLonLat
                            + "&destination="
                            + endLonLat;
            String queryResult = getResponse(queryUrl);
            JSONObject job = JSONObject.parseObject(queryResult);
            JSONArray ja = job.getJSONArray("results");
            JSONObject jobO = JSONObject.parseObject(ja.getString(0));
            result = Long.parseLong(jobO.get("distance").toString());
            return result;
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }
 
 
    /**
     * 功能描述: 根据两个定位点的经纬度算出两点间的距离(千米/km)
     *
     * @param startLonLat 起始经纬度
     * @param endLonLat   结束经纬度(目标经纬度)
     * @return java.lang.Long 两个定位点之间的距离
     */
    public String getDistanceTOKilometer(String startLonLat, String endLonLat) {
        try {
            // 返回起始地startAddr与目的地endAddr之间的距离,单位:米
            Long result = new Long(0);
            String queryUrl =
                    "http://restapi.amap.com/v3/distance?key=" + key + "&origins=" + startLonLat
                            + "&destination="
                            + endLonLat;
            String queryResult = getResponse(queryUrl);
            JSONObject job = JSONObject.parseObject(queryResult);
            JSONArray ja = job.getJSONArray("results");
            if (ja.size() == 0) {
                return 0.00 + "";
            }
            JSONObject jobO = JSONObject.parseObject(ja.getString(0));
            result = Long.parseLong(jobO.get("distance").toString());
            DecimalFormat decimalFormat = new DecimalFormat("#.00");
            double demo = result / 1000;
            return decimalFormat.format(demo);
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }
 
    /**
     * 功能描述: 发送请求
     *
     * @param serverUrl 请求地址
     * @return java.lang.String
     * @author isymikasan
     * @date 2022-01-26 09:15:01
     */
    private static String getResponse(String serverUrl) {
        // 用JAVA发起http请求,并返回json格式的结果
        StringBuffer result = new StringBuffer();
        try {
            URL url = new URL(serverUrl);
            URLConnection conn = url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
            in.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result.toString();
    }
 
 
}