doum
2 天以前 f0beadb9023ff20dc513ece3bcbd10051807663f
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
package com.doumee.core.tsp;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.HttpsUtil;
import com.doumee.dao.business.model.JkCustomer;
import com.doumee.dao.business.model.JkSketchCustomer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
 
@Slf4j
public class DistanceCalculator {
    private static final double EARTH_RADIUS = 6371.0; // 地球半径(单位:公里)
 
    public static void main(String[] args) {
        //118.363428,31.326485
        //118.363312,31.326638
        System.out.println(calculateDistance(39.326638d,116.363312d,31.326606,118.363272));
    }
 
    public static DistanceCustomerModel calculateDistanceGaode(String urlStr, JkCustomer c1, JkCustomer c2) {
        DistanceCustomerModel r =new DistanceCustomerModel();
        r.setStart(c1);
        r.setEnd(c2);
        r.setDistance(0);
        r.setCode(0);
        r.setLocations( new ArrayList<>());
        try {
            String url  =urlStr.replace("${lat1}", Constants.formatBigdecimalScale(c1.getLatitude(),6)+"")
                    .replace("${lat2}",Constants.formatBigdecimalScale(c2.getLatitude(),6)+"")
                    .replace("${long1}",Constants.formatBigdecimalScale(c1.getLongitude(),6)+"")
                    .replace("${long2}",Constants.formatBigdecimalScale(c2.getLongitude(),6)+"");
            String result = HttpsUtil.get(url ,true);
            JSONObject json = JSONObject.parseObject(result);
            if(json!=null
                    && json.getInteger("status")!=null
                    && json.getInteger("status") ==1
                    && json.getJSONObject("route")!=null
                    && json.getJSONObject("route").getJSONArray("paths") !=null
                    && json.getJSONObject("route").getJSONArray("paths") .size()>0 ){
                JSONArray array = json.getJSONObject("route").getJSONArray("paths");
                JSONObject model = array.getJSONObject(0);//取第一个
               Long distance = Long.parseLong(model.getString("distance"));
               r.setDistance(distance);
               JSONArray steps = model.getJSONArray("steps");
               String tl = "";
               if(steps!=null && steps.size()>0){
                   for (int i = 0; i < steps.size(); i++) {
                       if(StringUtils.isBlank(steps.getJSONObject(i).getString("polyline" ))){
                           continue;
                       }
                       if(!tl.equals("") &&!tl.endsWith(";")){
                           tl += ";";
                       }
                       tl+= steps.getJSONObject(i).getString("polyline" );
                   }
               }
               r.setPolyline(tl);
               r.setLocations(Arrays.asList(tl.split(";")));
               r.setCode(1);
               log.error("获取交通规划线路信息成功==============");
            }else{
                log.error("获取交通规划线路信息成功=====失败!!=========");
            }
        }catch (Exception e){
            log.error("获取交通规划线路信息成功=====失败==========");
        }
 
        return r ;
    }
    public static DistanceModel calculateDistanceGaode(String urlStr, JkSketchCustomer c1, JkSketchCustomer c2) {
        DistanceModel r =new DistanceModel();
        r.setStart(c1);
        r.setEnd(c2);
        r.setDistance(0);
        r.setCode(0);
        r.setLocations( new ArrayList<>());
        try {
            String url  =urlStr.replace("${lat1}", Constants.formatBigdecimalScale(c1.getLatitude(),6)+"")
                    .replace("${lat2}",Constants.formatBigdecimalScale(c2.getLatitude(),6)+"")
                    .replace("${long1}",Constants.formatBigdecimalScale(c1.getLongitude(),6)+"")
                    .replace("${long2}",Constants.formatBigdecimalScale(c2.getLongitude(),6)+"");
            String result = HttpsUtil.get(url ,true);
            JSONObject json = JSONObject.parseObject(result);
            if(json!=null
                    && json.getInteger("status")!=null
                    && json.getInteger("status") ==1
                    && json.getJSONObject("route")!=null
                    && json.getJSONObject("route").getJSONArray("paths") !=null
                    && json.getJSONObject("route").getJSONArray("paths") .size()>0 ){
                JSONArray array = json.getJSONObject("route").getJSONArray("paths");
                JSONObject model = array.getJSONObject(0);//取第一个
               Long distance = Long.parseLong(model.getString("distance"));
               r.setDistance(distance);
               JSONArray steps = model.getJSONArray("steps");
               String tl = "";
               if(steps!=null && steps.size()>0){
                   for (int i = 0; i < steps.size(); i++) {
                       if(StringUtils.isBlank(steps.getJSONObject(i).getString("polyline" ))){
                           continue;
                       }
                       if(!tl.equals("") &&!tl.endsWith(";")){
                           tl += ";";
                       }
                       tl+= steps.getJSONObject(i).getString("polyline" );
                   }
               }
               r.setPolyline(tl);
               r.setLocations(Arrays.asList(tl.split(";")));
               r.setCode(1);
               log.error("获取交通规划线路信息成功==============");
            }else{
                log.error("获取交通规划线路信息成功=====失败!!=========");
            }
        }catch (Exception e){
            log.error("获取交通规划线路信息成功=====失败==========");
        }
 
        return r ;
    }
 
    private BigDecimal getDecimalByVal(String val) {
        try {
            return new BigDecimal(val);
        }catch (Exception e){
 
        }
        return null;
    }
    public static long calculateDistance (double lat1, double lon1, double lat2, double lon2) {
        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);
 
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                        Math.sin(dLon / 2) * Math.sin(dLon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
 
        return  (long) (EARTH_RADIUS * c * 1000);
    }
}