puzhibing
2023-02-16 504fdd09f7ae6278308156ad416a35fc02ecb8b8
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.supersavedriving.driver.modular.system.util.GaoDe;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.supersavedriving.driver.modular.system.util.httpClinet.HttpClientUtil;
import com.supersavedriving.driver.modular.system.util.httpClinet.HttpResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.HashMap;
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;
    }
}