puzhibing
2023-05-22 c588d0fb5d7b61611b13911e4f0b65e760a7e862
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
package com.agentdriving.driver.modular.system.util.GaoDe;
 
import com.agentdriving.driver.modular.system.util.GaoDe.model.District;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.agentdriving.driver.modular.system.util.httpClinet.HttpClientUtil;
import com.agentdriving.driver.modular.system.util.httpClinet.HttpResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
* 地图工具类
* @author pzb
* @Date 2023/2/16 18:48
*/
public class MapUtil {
 
    public static Logger logger = LoggerFactory.getLogger("ServiceLog");
 
 
 
 
    /**
     * 获取两点的距离
     * @param origins       起点坐标
     * @param destination   终点坐标
     * @param type          计算类型:0:直线距离 1:驾车导航距离(仅支持国内坐标)。
     * @return
     */
    public static Map<String, String> getDistance(String origins, String destination, Integer type){
        try {
            String url = "https://restapi.amap.com/v3/distance?key=" + MapConfig.key + "&origins=" + origins + "&destination=" + destination +
                    "&type=" + type;
            HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", url, null, null, "json");
            if(httpResult.getCode() != 200){
                logger.debug(httpResult.getData());
                return null;
            }
            String data = httpResult.getData();
            JSONObject jsonObject = JSON.parseObject(data);
            String status = jsonObject.getString("status");
            if(status.equals("1")){
                JSONArray results = jsonObject.getJSONArray("results");
                JSONObject jsonObject1 = results.getJSONObject(0);
                Map<String, String> map = new HashMap<>();
                map.put("distance", jsonObject1.getString("distance"));//距离(米)
                map.put("duration", jsonObject1.getString("duration"));//预计时间(秒)
                return map;
            }else{
                logger.debug(data);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
 
 
    /**
     * 地址转换经纬度
     * @param address
     * @return
     */
    public static List<String> geocoding(String address){
        try {
            String url = "https://restapi.amap.com/v3/geocode/geo?key=" + MapConfig.key + "&output=JSON&address=" + address;
            HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", url, null, null, "json");
            if(httpResult.getCode() != 200){
                return null;
            }
            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);
                }
            }
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
 
 
 
    /**
     * 根据经纬度获取行政区域信息
     * @param lon
     * @param lan
     * @return
     * @throws Exception
     */
    public static District geocode(String lon, String lan) {
        try {
            String url = "https://restapi.amap.com/v3/geocode/regeo?key=" + MapConfig.key + "&location=" + lon + "," + lan;
            HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", url, null, null, "json");
            if(httpResult.getCode() != 200){
                return null;
            }
            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");
                District district1 = new District();
                district1.setProvince(province);
                district1.setProvinceCode(code.substring(0, 2) + "0000");
                district1.setCity(city);
                district1.setCityCode(code.substring(0, 4) + "00");
                district1.setDistrict(district);
                district1.setDistrictCode(code);
                return district1;
            }
            logger.debug(httpResult.getData());
            return null;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}