luodangjia
2024-11-05 4dd69a0476fc2b9e1cff291611d7e56fbe4324ec
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
package com.ruoyi.common.core.utils;
 
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.core.enums.GaoDeEnum;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.vo.*;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @author HJL
 */
public class GaoDeMapUtil {
 
    /**
     * 功能描述: 高德地图Key
     */
    private static final String GAO_DE_KEY = "2f22a10b75e0c24a074de515f3cdcaea";
 
    /**
     * 功能描述: 根据地址名称得到两个地址间的距离
     *
     * @param start 起始位置
     * @param end   结束位置
     * @return long 两个地址间的距离
     */
    public static Long getDistanceByAddress(String start, String end) {
        String startLonLat = getLonLat(start).getDatas();
        String endLonLat = getLonLat(end).getDatas();
        return Long.valueOf(getDistance(startLonLat, endLonLat).getDatas().toString());
    }
 
    /**
     * 功能描述: 地址转换为经纬度
     *
     * @param address 地址
     * @return 经纬度
     */
    public static Result<String> getLonLat(String address) {
        try {
            // 返回输入地址address的经纬度信息, 格式是 经度,纬度
            String queryUrl = "http://restapi.amap.com/v3/geocode/geo?key=" + GAO_DE_KEY + "&address=" + address;
            // 高德接口返回的是JSON格式的字符串
            String queryResult = getResponse(queryUrl);
            JSONObject job = JSONObject.parseObject(queryResult);
            JSONObject json = JSONObject
                    .parseObject(
                            job.get("geocodes").toString().substring(1, job.get("geocodes").toString().length() - 1));
            String lngAndLat = json.get("location").toString();
            return Result.succeed(lngAndLat, "经纬度转换成功!");
        } catch (Exception e) {
            return Result.failed(e.toString());
        }
    }
 
    /**
     * 将经纬度 转换为 地址
     *
     * @param longitude 经度
     * @param latitude  纬度
     * @return 地址名称
     */
    public static Result<String> getAddress(String longitude, String latitude) {
        String url;
        try {
            url = "http://restapi.amap.com/v3/geocode/regeo?output=JSON&location=" + longitude + "," + latitude
                    + "&key=" + GAO_DE_KEY + "&radius=0&extensions=base";
            // 高德接口返回的是JSON格式的字符串
            String queryResult = getResponse(url);
            if (ObjectUtils.isNull(queryResult)) {
                return Result.failed("查询结果为空");
            }
            // 将获取结果转为json 数据
            JSONObject obj = JSONObject.parseObject(queryResult);
            if (obj.get(GaoDeEnum.STATUS.getCode()).toString().equals(GaoDeEnum.INT_ONE.getCode())) {
                // 如果没有返回-1
                JSONObject reGeoCode = obj.getJSONObject(GaoDeEnum.RE_GEO_CODE.getCode());
                if (reGeoCode.size() > 0) {
                    // 在regeocode中拿到 formatted_address 具体位置
                    String formatted = reGeoCode.get("formatted_address").toString();
                    JSONObject addressComponent = reGeoCode.getJSONObject("addressComponent");
                    String adcode = addressComponent.getString("adcode");
                    String s = convertToCityCode(adcode);
                    System.err.println("====s");
                    return Result.succeed(formatted, "地址获取成功!");
 
                } else {
                    return Result.failed("未找到相匹配的地址!");
                }
            } else {
                return Result.failed("请求错误!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("系统未知异常,请稍后再试");
        }
    }
 
 
    public static Result<String> getCityCode(String longitude, String latitude) {
        String url;
        try {
            url = "http://restapi.amap.com/v3/geocode/regeo?output=JSON&location=" + longitude + "," + latitude
                    + "&key=" + GAO_DE_KEY + "&radius=0&extensions=base";
            // 高德接口返回的是JSON格式的字符串
            String queryResult = getResponse(url);
            if (ObjectUtils.isNull(queryResult)) {
                return Result.failed("查询结果为空");
            }
            // 将获取结果转为json 数据
            JSONObject obj = JSONObject.parseObject(queryResult);
            if (obj.get(GaoDeEnum.STATUS.getCode()).toString().equals(GaoDeEnum.INT_ONE.getCode())) {
                // 如果没有返回-1
                JSONObject reGeoCode = obj.getJSONObject(GaoDeEnum.RE_GEO_CODE.getCode());
                if (reGeoCode.size() > 0) {
                    // 在regeocode中拿到 formatted_address 具体位置
                    String formatted = reGeoCode.get("formatted_address").toString();
                    JSONObject addressComponent = reGeoCode.getJSONObject("addressComponent");
                    String adcode = addressComponent.getString("adcode");
                    String s = convertToCityCode(adcode);
                    System.err.println("====s");
                    return Result.succeed(s, "地址获取成功!");
 
                } else {
                    return Result.failed("未找到相匹配的地址!");
                }
            } else {
                return Result.failed("请求错误!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("系统未知异常,请稍后再试");
        }
    }
 
    public static String convertToCityCode(String districtCode) {
        // 确保输入的字符串长度至少为6位
        if (districtCode.length() >= 6) {
            // 截取前6位中的前4位作为城市代码
            return districtCode.substring(0, 4) + "00";
        } else {
            throw new IllegalArgumentException("Invalid district code length: " + districtCode);
        }
    }
 
 
    /**
     * 功能描述: 根据两个定位点的经纬度算出两点间的距离
     * 经纬度格式为: 经度,纬度
     *
     * @param startLonLat 起始经纬度
     * @param endLonLat   结束经纬度(目标经纬度)
     * @return 两个定位点之间的距离
     */
    public static Result<Long> getDistance(String startLonLat, String endLonLat) {
        System.err.println("======"+startLonLat+"===="+endLonLat);
        // 返回起始地startAddr与目的地endAddr之间的距离,单位:米
        String queryUrl =
                "http://restapi.amap.com/v3/distance?key=" + GAO_DE_KEY + "&origins=" + startLonLat
                        + "&destination="
                        + endLonLat;
        String queryResult = getResponse(queryUrl);
        System.err.println("======"+queryResult);
        JSONObject job = JSONObject.parseObject(queryResult);
        JSONArray ja = job.getJSONArray("results");
        JSONObject jobO = JSONObject.parseObject(ja.getString(0));
        long result = Long.parseLong(jobO.get("distance").toString());
        return Result.succeed(result, "距离计算成功!");
    }
 
    /**
     * 功能描述: 根据地址获取城市code
     *
     * @param address d地址
     * @return 两个定位点之间的距离
     */
    public static Result<CityInfoVO> getAddressInfo(String address) {
        // 返回起始地startAddr与目的地endAddr之间的距离,单位:米
        String queryUrl = "https://restapi.amap.com/v3/geocode/geo?address=" + address
                + "&output=json&key=" + GAO_DE_KEY;
        String queryResult = getResponse(queryUrl);
        GaoDeAddressInfoVO data = JSONObject.parseObject(queryResult, GaoDeAddressInfoVO.class);
        List<Geocodes> geocodes = data.getGeocodes();
        Geocodes info = geocodes.get(0);
        String adcode = info.getAdcode();
        String city = info.getCity();
        String location = info.getLocation();
        String[] split = location.split(",");
        return Result.succeed(new CityInfoVO(city, adcode,Double.valueOf(split[0]),Double.valueOf(split[1])), "距离计算成功!");
    }
 
    /**
     * 功能描述: 发送请求
     *
     * @param serverUrl 请求地址
     * @return 请求结果
     */
    private static String getResponse(String serverUrl) {
        // 用JAVA发起http请求,并返回json格式的结果
        StringBuilder result = new StringBuilder();
        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 (IOException e) {
            e.printStackTrace();
        }
        return result.toString();
    }
 
    /**
     * 从出发地触发到目的地路线规划
     *
     * @param origin      出发地经纬度
     * @param destination 目的地经纬度
     * @return 路线规划信息
     */
    public static Path routing(String origin, String destination) {
        String baseUrl = "https://restapi.amap.com/v3/direction/driving?origin=%s&destination=%s" +
                "&extensions=all&output=json&key=%s";
        String requestUrl = String.format(baseUrl, origin, destination, GAO_DE_KEY);
        StringBuilder json = new StringBuilder();
        try {
            URL url = new URL(requestUrl);
            URLConnection urlConnection = url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),
                    StandardCharsets.UTF_8));
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (IOException e) {
            throw new GlobalException("获取路线规划失败!");
        }
        String data = json.toString();
        GaoDeMap gaoDeMap = JSONObject.parseObject(data, GaoDeMap.class);
        Path path = gaoDeMap.getRoute().getPaths().get(0);
        List<Steps> steps = path.getSteps();
        for (Steps step : steps) {
            // 经纬度地址指向
            List<PolylineData> polylineDataList = new ArrayList<>();
            String polyline = step.getPolyline();
            List<String> list = Arrays.stream(polyline.split(";")).collect(Collectors.toList());
            for (String s : list) {
                String[] split = s.split(",");
                // 经纬度
                String longitude = split[0];
                String latitude = split[1];
                polylineDataList.add(new PolylineData(longitude, latitude));
            }
            step.setPolylineList(polylineDataList);
        }
        return path;
    }
 
}